call和apply

2019-07-29  本文已影响0人  李友胜
实现一个call函数
Function.prototype.mycall = function (context) {
    if (typeof this !== 'function') {
        throw new TypeError('非函数')
    }
    context = context || window;
    context.fn = this;
    let arg = [...arguments].slice(1)
    let result = context.fn(...arg)
    delete context.fn
    return result
}

var person = {
    name: 'person',
    getName: function () {
        console.log(this.name)
    }
}

var boy = {
    name: 'boy'
}

person.getName.mycall(boy)
上一篇 下一篇

猜你喜欢

热点阅读