js实现call方法

Function.prototype.call6 = function(content = window) {
    content.fn = this;
    const args = [...arguments].slice(1);
    const result = content.fn(...args);
    console.log(this) // bar函数
    console.log(content) // foo对象
    delete content.fn;
    return result;
}

let foo = {
    value: 1
}

function bar(name, age) {
    console.log(name);
    console.log(age);
    console.log(this.value);
}

bar.call6(foo, 'LY', 25);

猜你喜欢

转载自blog.csdn.net/bangbDIV/article/details/89840884