[关闭]
@panhonhang 2019-12-13T20:57:24.000000Z 字数 444 阅读 497

bind apply call实现

手撕代码


实现bind

Function.prototype.bind1 = function(context){
    let fn = this;

    let arg = [...arguments];
    arg.shift();

    return function(){
        return fn.apply(context,arg)
    }
}

let a = function(name){
    console.log(this.value);
    console.log(name);
}

let b = {
    value: 1,
}

let c = {
    value: 3
}

let a2 = a.bind1(b,'aa')();

实现apply函数

Function.prototype.apply1 = function(context){
    context.fn = this;
    let list = [...arguments];
    list.shift();
    let result = context.fn(...list[0]);
    delete context.fn;
    return result;
}
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注