js implements call, apply and bind

call:

Function.prototype.$call = function(context) {
    var context = context || window;
    context.fn = this;
    var args = Array.from(arguments).slice(1)
    context.fn(...args)
    delete context.fn;
}

 

apply:

Function.prototype.$apply = function(context) {
    var context = context || window;
    context.fn = this;
    var args = Array.from(arguments[1])
    context.fn(...args)
    delete context.fn;
}

 

bind:

Function.prototype.$apply = function(context) {
    var context = context || window;
    context.__proto__.fn = this;
    var args = Array.from(arguments).slice(1)
    return function(){
        var args2 = Array.from(arguments)
        context.fn(...args,...args2)
        delete context.__proto__.fn
    }
}

 

Guess you like

Origin www.cnblogs.com/AwenJS/p/12695283.html