MDNバインドポリフィルを以下に示します。MDNバインドポリフィルの線の説明
私はfToBind.apply
呼び出しで
this instanceof fNOP ? this : oThis
の目的をうまくしようとしています。
私はそれの周りに頭を上げることはできません。誰かが光を放つのを助けることができますか?
Function.prototype.bindMdn = 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, aArgs.concat(Array.prototype.slice.call(arguments)));
}
;
if (this.prototype) {
// Function.prototype doesn't have a prototype property
fNOP.prototype = this.prototype;
}
fBound.prototype = new fNOP();
return fBound;
};
バインド機能を呼び出すときにバインドされた機能のインスタンスを対象として提供されている場合、短絡のようですが、typeofのチェックはこれをキャッチしなければならないので、私はその存在を理解していません。 MDNページへ
リンク:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind
編集:これは示唆し、重複は異なる問題です。提案された複製はなぜfNOP
が必要かを尋ねます。私は完全にそれをgrok。
この質問は、instanceof
チェックが必要な理由とその機能について説明しています。上記の短絡仮説とそれが完全に意味をなさない理由を提示する。
MDNページのリンクを追加してもよろしいですか? –
@mortezaT [MDN:Function.prototype.bind()Polyfill](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Function/bind#Polyfill) –
恐らく[ MDN Function.prototype.bindバインドされた関数がコンストラクタとして呼び出されました](http://stackoverflow.com/questions/23693282) –