// call 
Function.prototype.call = function (context, ...args) {
    context = context || window
    const fnSymbo = Symbol('fn')
    context[fnSymbo] = this
    let fn = context[fnSymbo](...args)

    delete context[fnSymbo]

    return fn
}
var obj = { name: 'cedric' }

function fn(age, address) {
    console.log(`${this.name} is ${age},in ${address}`)
}

fn.call(obj, 29, '上海')  // cedric is 29,in 上海
// apply 
Function.prototype.apply = function (context, argsArr) {
    context = context || window;

    const fnSymbol = Symbol("fn");
    context[fnSymbol] = this;

    let fn = context[fnSymbol](...argsArr);
    delete context[fnSymbol];
    return fn
}
var obj = { name: 'cedric' }

function fn(age, address) {
    console.log(`${this.name} is ${age},in ${address}`)
}

fn.apply(obj, [29, '上海'])  // cedric is 29,in 上海
// bind
Function.prototype.bind = function (context, ...args) {
    context = context || window;
    const fnSymbol = Symbol("fn");
    context[fnSymbol] = this;

    return function (..._args) {
        args = args.concat(_args);

        context[fnSymbol](...args);
        delete context[fnSymbol];
    }
}
var obj = { name: 'cedric' }

function fn(age, address) {
    console.log(`${this.name} is ${age},in ${address}`)
}

fn.bind(obj, 29, '上海')()  // cedric is 29,in 上海

相关文章: