首页 > 移动平台 > 详细

js实现call,apply和bind

时间:2020-04-14 10:56:50      阅读:68      评论:0      收藏:0      [点我收藏+]

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

原文:https://www.cnblogs.com/AwenJS/p/12695283.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!