call、apply、bind的源码模拟

1、call

Function.prototype.customCall = function(ctx){
    ctx.fn = this;
    let args = [...arguments].slice(1);
    let result = ctx.fn(...args);
    delete ctx.fn;
    return result
}

2、apply

Function.prototype.customApply = function(ctx){
    ctx.fn = this;
    let result = arguments[1] ? ctx.fn(arguments[1]) : ctx.fn();
    delete ctx.fn;
    return result
}

3、bind

Function.prototype.customBind = function(ctx){
    let that = this;
    let args = [...arguments].slice(1);
    return function(){
        that.apply(ctx, args);
    }
}

 

  

call、apply、bind的源码模拟

上一篇:Android搭建环境和android工程基本信息;android的xml代码自动提示功能


下一篇:使用WordPress搭建个人手机博客(阿里云)