手写模仿实现call、apply、bind的实现。
//call的实现
Function.prototype.mycall=function(context,...args){
context=context||window;
context.fn=this;
delete context.fn(...args);
return;
}
//apply的实现
Function.prototype.myapply=function(context,args){
context=context||window;
context.fn=this;
delete context.fn(args);
}
//bind的实现
Function.prototype.mybind=function(context){
let self=this;
function noop(){}
let args=Array.prototype.slice.call(arguments,1);
let myBound=function(){
let myargs=Array.prototype.slice.call(arguments);
self.apply(this instanceof noop?this:context,args.concat(myargs));
}
noop.prototype=this.prototype;
myBound.prototype=new noop();
return myBound;
}