首页 > 移动平台 > 详细

call、apply、bind的源码模拟

时间:2020-06-27 20:44:34      阅读:67      评论:0      收藏:0      [点我收藏+]

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的源码模拟

原文:https://www.cnblogs.com/jyybeam/p/13199649.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!