编辑代码

// 思路:
// 在call的对象中添加调用call的函数
// 调用函数
// 删除函数
Function.prototype.apply2 = function(context,args){
    let cont = context || window;   //context为null时,this为全局的window
    cont.fn = this; //添加函数
    let res
    if(!args){
        res = cont.fn();    //调用函数,并保存结果
    }
    else{
        res = cont.fn(...args); //调用函数,并保存结果
    }
    delete cont.fn; //删除函数
    return res  //返回结果
}
let obj = {
    value:1
}
function bar(name,age){
    return {
        value:this.value,
        name:name,
        age:age
    }
}
console.log(bar.apply2(obj,['kevin',19]))