0%

手写bind

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Function.prototype.bind=function(context){
if(typeof this !=='function'){
throw new Error("Function .prototype.bind-what is trying to be bound is not callable")
}
var self=this;
var args=Array.prototype.slice.call(arguments,1);
var fNOP=function(){};
var fBound=function(){
var bindArgs=Array.prototype.slice.call(arguments);
return self.apply(this instanceof fNOP?this:context,args.concat(bindArgs));
}
fNOP.prototype=this.prototype;
fBound.prototype=new fNOP();
return fBound;

}

参考:

[]: https://github.com/mqyqingfeng/Blog/issues/12