记住 bind 函数一共两个作用:1. 改变 this 指向;2. 暂存参数;
Function.prototype.bind2=function(content, ...arg) {
let func = this;
return function() {
return func.apply(content, [...arg, ...Array.from(arguments)])
}
}
// 验证
function add(a, b) {
console.log(this.num);
console.log(a, b);
return a + b;
}
let obj = {num: 1}
var func = add.bind2(obj, 1);
console.log(func(2));