big-show
8/8/2019 - 3:51 AM

call

call

{
    Function.prototype.call2 = function(...rest){
        let context = rest[0];
        context=context?context:null;
        let args = rest.slice(1);
        context.fn=this;
        let res = context.fn(...args);
        delete context.fn;
        return res;
    }
    function bar(name,age)
    {
        console.log(this.value);
        console.log(name,age);
        return {
            name:name,
            age:age,
            value:this.value
        }
    }
    let obj = {
        value:66
    }
    let hh = bar.call2(obj,"liu",22);
    console.log(hh);
}