一、bind特性
- 第一个参数用来改变this指向
- bind第二个之后的参数将作为内部函数的参数列表
- bind方法返回一个方法,并满足柯里化(仍可以传递参数)
- bind内部方法如果使用new进行实例化,this指向新实例对象
- 返回的函数对象可以使用原方法原型链上的属性或方法
二、实现
Function.prototype.mybind = function(obj){
let self = this
const [,...args]=arguments
function a(){
return self.apply(this instanceof a? this:obj,[...args,...arguments])
}
a.prototype = self.prototype
return a
}
三、测试
var name ='我'
let obj = {
name:'你',
say:funciton(){
console.log('who?',this.name)
}
}
obj.say.mybind(null)()//第一个参数为null或者undefined,this指向window
obj.say.bind(null)()