記住 bind 函數(shù)一共兩個(gè)作用:1. 改變 this 指向;2. 暫存參數(shù);
Function.prototype.bind2=function(content, ...arg) {
let func = this;
return function() {
return func.apply(content, [...arg, ...Array.from(arguments)])
}
}
// 驗(yàn)證
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));