4
if (!Function.prototype.bind) {
    Function.prototype.bind = function (oThis) {
        if (typeof this !== "function") {
            // closest thing possible to the ECMAScript 5 internal IsCallable function  
            throw new TypeError("Function.prototype.bind - 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 || window, aArgs.concat(Array.prototype.slice.call(arguments)));
            };

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

        return fBound;
    };
}

これはバインドMDCからの選択です。何が行われているのかわかりませんthis instanceof fNOP ? this。誰か教えてくれませんか?oThis || window直接使ってみませんか?

4

1 に答える 1

1
return fToBind.apply(this instanceof fNOP ? this : oThis || window, aArgs.concat(Array.prototype.slice.call(arguments)));

thisが のインスタンスである場合、fNOP最初の引数は になりますthis。そうでない場合は、それがoThis「真実」(null ではなく、未定義ではなく、false と同義のもの) であるか、またはwindowofoThisが false である場合になります。

このコードの省略形です:

if(this instanceof fNOP){
    firstparameter = this;
} else {
    if(oThis){
        firstParameter = oThis;
    } else {
        firstParameter = window;
    }
}
于 2012-04-13T10:18:25.150 に答える