SOURCE

// 实现call
Function.prototype.mycall = function () {
    debugger;
    let [thisArg, ...args] = [...arguments];
    thisArg = Object(thisArg) || window;
    let fn = Symbol();
    thisArg[fn] = this;
    let result = thisArg[fn](...args);
    delete thisArg[fn];
    return result;
}

// 实现apply
Function.prototype.myapply = function () {
    debugger;
    let [thisArg, args] = [...arguments];
    thisArg = Object(thisArg);
    let fn = Symbol();
    thisArg[fn] = this;
    let result = thisArg[fn](...args);
    delete thisArg.fn;
    return result;
}

//测试用例
let cc = {
    a: 1
}

function demo(x1, x2) {
    console.log(typeof this, this.a, this)
    console.log(x1, x2)
}

demo.apply(cc, [2, 3])
demo.myapply(cc, [2, 3])
demo.call(cc,33,44)
demo.mycall(cc,33,44)
console 命令行工具 X clear

                    
>
console