Function.prototype._call = function (context, ...args) {
// 判断context,如果为null或者undefined,直接指向window
let cxt = context || window;
// 新建一个唯一的Symbol,避免重复
let func = Symbol();
cxt[func] = this;
args = args ? args : [];
// 以对象的方式调用func,此时的this为传入需要绑定的this指向
const res = args.length > 0 ? cxt[func](args) : cxt[func]();
// 删除方法,避免对全局造成污染
delete cxt[func];
return res;
};

京公网安备 11010502036488号