手动实现bind

123 阅读1分钟

实现bind功能其实就是返回一个绑定了一个作用域的函数

    Function.prototype.bind2 = function(context){
        let self = this 
        return function(...val){
            return self.apply(context, val)
        }
    }
    
    function getV(...val){
        console.log(...val)
        return this.value
    }
    let obj = {
        value: 456
    }
    getV.bind2(obj)(789, 0)  //  打印798 0  返回456