美文网首页html5
自己实现一个bind函数

自己实现一个bind函数

作者: 前端小旋风 | 来源:发表于2020-07-20 16:11 被阅读0次
    Function.prototype.myBind = function (){
        if(typeof this !== 'function') throw new TypeError('@');
        var fn_self = this,
            fn_args = Array.prototype.slice.call(arguments,1);
        var fn_empty = function (){}
        var fn = function (){
            return fn_self.apply(this instanceof fn ? this : arguments[0],fn_args.concat(Array.prototype.slice.call(arguments)))
        }
        if(this.prototype){
            fn_empty.prototype = this.prototype;
        }
        fn.prototype = new fn_empty();
        return fn;
    }
    

    相关文章

      网友评论

        本文标题:自己实现一个bind函数

        本文链接:https://www.haomeiwen.com/subject/mbrykktx.html