SOURCE

Function.prototype.myCall = function (context) {
    var context = context || window
    var arg = [...arguments].slice(1)
    context.fn = this
    var result = context.fn(...arg)
    delete context.fn
    return result
}

Function.prototype.myApply = function (context = window, args) {
    let key = Symbol('key')
    context[key] = this
    let result = context[key](...args)
    delete context[key]
    return result
}

Function.prototype.myBind = function (context) {
    if (typeof this !== 'function') {
        throw new TypeError('Error')
    }
    var _this = this
    var args = [...arguments].slice(1)
    // 返回一个函数
    return function F() {
        // 因为返回了一个函数,我们可以 new F(),所以需要判断
        if (this instanceof F) {
            return new _this(...args, ...arguments)
        }
        return _this.apply(context, args.concat(...arguments))
    }
}

let a = {
    value: 1
}
function getValue(name, age) {
    console.log(name)
    console.log(age)
    console.log(this.value)
}
// getValue.call(a, 'poe', '24')
// getValue.myCall(a, 'poe', '24')

// getValue.apply(a, ['poe', '24'])
// getValue.myApply(a, ['poe', '24'])

// getValue.bind(a, 'poe', '24')()
getValue.myBind(a, 'poe', '24')()
console 命令行工具 X clear

                    
>
console