let obj1 = {
num: 1,
sum(a, b, c, d) {
console.log(this, "->", a, arguments);
return this.num + a + b + (c ? c : 0) + (d ? d : 0);
},
};
let obj2 = {
num: 10,
};
Function.prototype.myBind = function () {
let context = arguments[0], //要绑定的 this指向
arg = [...arguments].slice(1), // 和this执行一起传进的参数
that = this;
return function () {
//合并bing时传入的参数 和调用方法时传入的参数
let argAry = [].concat.apply([...arguments], arg);
return that.apply(context, argAry); //改变this指向
};
};
obj1.sum.myBindF(obj2)(2, 3, 5)
12-27
235
07-30
200