一、核心思想
bind相比apply和call引入了闭包的思想,重点是正确找到this和arguments
二、代码实现
/*** 手写bind* @param {Function} o 函数* @param {Array} ...args1 数组* @return 根据具体情况考虑*/
function Fn(a,b,c,d){console.log("this.name:",this.name)console.log("a,b,c,d:",a,b,c,d)
}
let obj = {name:'ww'
}
Function.prototype.myBind = function(o,...args1){o = o||window//这里this指向是Fnlet that = thisreturn function(){//这里arguments指向(3,4)that.apply(o,[...args1,...arguments])}
}
Fn.myBind(obj,1,2)(3,4)
// this.name: ww
// a,b,c,d: 1 2 3 4