美文网首页
bind简易实现

bind简易实现

作者: mengxr | 来源:发表于2017-08-02 16:49 被阅读22次
//1.原生判断
Function.prototype.bind = Function.prototype.bind || function (context) {
    
    //2.先验证类型
    if (typeof this !== 'function') {

        throw new Error('Function.prototype.bind need function');

    }
    //3.保存this
    const self = this;
    //4.保存第一部分参数
    const argFirst = Array.prototype.slice.call(arguments, 1);

    return function () {
        //第二部分参数
        const argSecond = Array.prototype.slice.call(arguments);
        //5.合并参数
        return self.apply(context, argFirst.concat(argSecond));

    }

}

相关文章

网友评论

      本文标题:bind简易实现

      本文链接:https://www.haomeiwen.com/subject/zxkylxtx.html