bind方法封装

198 阅读1分钟

伪数组转真数组

ES5

Array.prototype.slice.call(arguments)

[].slice.call(arguments)

ES6

[...arguments]

Array.from(arguments)

bind是函数原型上的方法,而且与call和apply不同点在于他不会执行函数,所以要在函数上添加,并且返回一个函数

Function.prototype.myBind = function(rest){
    let that = this;
    let arg = [...arguments];
    return function(){
        that.apply(rest,arg)
    }
}