Ich versuche zu verstehen, wie sich jQuery selbst aufstellt.JQuery-Bibliothek Modul-Export
Gleich zu Beginn ruft jQuery automatisch eine Funktion auf, die ein Modul exportiert.
Wie funktioniert das Setup?
Hier einige detailliertere Unterfragen, die die die allgemeinere Frage beantworten könnten:
- Was
function(w)
beimodule.exports
die Verwendung des rekursiven Aufruf ist? - Was nutzt die Variable
noGlobal
? - Wo ist die
factory
tatsächlich eingerichtet und was ist ihr Typ? - Warum kann das
factory
Argument mit einem Argument und mit zwei auch aufgerufen werden? - Was soll das
global
Argument enthalten? (Ich wünschte, es ist eine Art, wie in C++ war ...)
(function(global, factory) {
if (typeof module === "object" && typeof module.exports === "object") {
// For CommonJS and CommonJS-like environments where a proper `window`
// is present, execute the factory and get jQuery.
// For environments that do not have a `window` with a `document`
// (such as Node.js), expose a factory as module.exports.
// This accentuates the need for the creation of a real `window`.
// e.g. var jQuery = require("jquery")(window);
// See ticket #14549 for more info.
module.exports = global.document ?
factory(global, true) :
function(w) {
if (!w.document) {
throw new Error("jQuery requires a window with a document");
}
return factory(w);
};
} else {
factory(global);
}
// Pass this if window is not defined yet
}(typeof window !== "undefined" ? window : this, function(window, noGlobal) {
eine weitere Frage: Woher kommt das w-Argument? – Beginner
@Beginner Wenn das globale Objekt kein 'Dokument' enthält, gibt es eine Funktion zurück, die ein Argument, das' w', akzeptiert. Dieses Objekt wäre ein "Fenster" -ähnliches Objekt mit einem "Dokument", das mit etwas wie jsdom erzeugt werden kann. –
Bedeutet dies, dass w eine globale Variable ist, die verfügbar ist, wenn kein Dokument verfügbar ist? – Beginner