js实现call,apply和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
    }
}

 

js实现call,apply和bind

上一篇:[Bug日记0001]命令行启动mysql报错


下一篇:深入浅出的掌握Mockito