Function.prototype.bind实现方法

200 阅读1分钟

zhuanlan.zhihu.com/p/49924137?…

\

Function.prototype.bind_=function(){

\

    var self = this,//保持原函数

\

    context = [].shift.call(arguments),//需要绑定的this上下文

\

    args = [].slice.call(arguments);//剩余的参数转成数组

\

    return function(){  //返回一个新的函数

\

        return self.apply_(context, [].concat.call(args, [].slice.call(arguments)));

\

        //执行新的而函数的时候,会把之前传入的context当做新函数体内的this

\

        //并且组合两次分别传入的参数,作为新函数的参数

\

    }

\

};