手写call apply bind

99 阅读1分钟
call
Function.prototype.CALL = function(){    
    let args = Array.from(arguments);    
    let obj = args.splice(0,1)[0];    
    obj.fn = this;    var result = obj.fn(...args);    
    return result    
    delete result
    }
const obj = {    
    b:'2324',    
    f:234
}
function objfn(){ 
   console.log(this);
}
objfn.CALL(obj,2,3,5);

Function.prototype.apply = function(context,args){    context.fn = this;    var result = obj.fn(args);    return result;    delete result;}const obj = {    b:'2324',    f:234}function objfn(){    console.log(this);}objfn.apply(obj,[2,3,5]);


bind
Function.prototype.bind= function(context,args){    const _this = this; // 此处this指向调用bind 的objfn    return function(){        return _this.apply(context,args)    }}const obj = {    b:'2324',    f:234}function objfn(){    console.log(this);}objfn.bind(obj,[2,3,5])()