实现 call 方法

Function.prototype.myCall = function(context) {
    if (typeof this !== 'function') {
        return
    }
    let args = [...arguments].slice(1)
    context = context || window || global
    context.fn = this
    let result = context.fn(...args)
    delete context.fn
    return result;
}

let person = {
    name: 'test'
}

function setInfo(age, address) {
    this.age = age
    this.address = address
}

setInfo.myCall(person, '12', '北京')
console.log('结果', person)

你可能感兴趣的:(javascript,开发语言,ecmascript)