实现call,apply以及不用call和apply方法模拟实现ES5的bind方法

Function.prototype.llqCall = function(o){
if(typeof this != 'function'){
throw TypeError('error')
}
let obj = o || global;
let args = Array.from(arguments).slice(1);
obj.fn = this;
let result = o.fn(...args);
delete obj.fn;
return result;

}

Function.prototype.llqApply = function(o){
if(typeof this != 'function'){
throw TypeError('error')
}
let obj = o || global;
let args = Array.from(arguments)[1];
console.log(args)
obj.fn = this;
let result = obj.fn(args);
delete obj;
return result;
}

Function.prototype.llqBind = function(o){
if(typeof this != 'function'){
throw TypeError('error');
}
let obj = o || global;
let _this = this;
let args = Array.from(arguments).slice(1);
let fBind = function(){
let self = this instanceof fBind ?this:obj;
_this.llqApply(self,args.concat(Array.from(arguments)))
}
let f = function(){}
f.prototype = this.prototype;
fBind.prototype = new f();
return fBind();

}

你可能感兴趣的:(实现call,apply以及不用call和apply方法模拟实现ES5的bind方法)