bind模拟

Posted chauvet

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bind模拟相关的知识,希望对你有一定的参考价值。

if (!Function.prototype.bind) {
        Function.prototype.bind = function(oThis) {
        if (typeof this !== ‘function‘) {
    
        throw new TypeError(‘What is trying to be bound is not callable‘);

    }
    var aArgs = Array.prototype.slice.call(arguments, 1),
        fToBind = this,
        fNOP = function() {},
        fBound = function() {
        return fToBind.apply(this.instanceof fNOP ? this : oThis,     aArgs.concat(Array.prototype.slice.call(arguments)));

        fNOP.prototype = this.prototype;
        fBound.prototype = new fNOP();

        return fBound;
        }
    }        
}

  

以上是关于bind模拟的主要内容,如果未能解决你的问题,请参考以下文章