call的特点
使用一个指定的this值和单独给出的一个或者多个参数来调用一个函数
自定义实现call
/// 定义在原型上,方便调用
Function.prototype.call = function(context, ...args) {
// 当this为null或者为undefined时,this指向window
let cx = context || window;
// 避免函数名与上下文冲突,使用Symbol定义
let func = new Symbol();
cx[func] = this;
/// 根据参数格式化
args = args.length > 0 ? args : [];
/// 根据不同的参数个数做处理
const result = args.length > 0 ? cx[func](...args) : cx[func]();
/// 删除方法,避免污染
delete cx[func];
return result;
}