Die MDN binden Polyfill ist unten gezeigt.Erklärung einer Linie in MDN binden Polyfill
Ich versuche, den Zweck der
this instanceof fNOP ? this : oThis
im fToBind.apply
Aufruf zu erarbeiten.
Ich kann mich nicht darum kümmern. Kann jemand helfen, etwas Licht zu geben?
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;
};
Es scheint ein Kurzschluss, wenn eine Instanz der gebundenen Funktion sein wird als das Ziel geliefert wird, wenn die gebundene Funktion aufruft, aber die typeof Überprüfung sollte diese fangen, so verstehe ich nicht seine Präsenz.
Link zur MDN Seite:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind
Edit: Dies ist eine andere Frage aus dem vorgeschlagenen Duplikat. Das vorgeschlagene Duplikat fragt, warum fNOP
benötigt wird. Ich grabe das völlig.
Diese Frage ist, warum die instanceof
Prüfung benötigt wird und welche Funktion es dient. Ich präsentiere meine Kurzschlusshypothese oben, zusammen mit einem Grund, warum das keinen vollen Sinn ergibt.
Würden Sie bitte den Link der MDN-Seite hinzufügen? –
@mortezaT [MDN: Function.prototype.bind() Polyfill] (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Function/bind#Polyfill) –
Wahrscheinlich ein Duplikat von [ MDN Function.prototype.bind gebundene Funktion aufgerufen als Konstruktor] (http://stackoverflow.com/questions/23693282) –