Sto cercando di capire come si imposta jQuery.Esportazione modulo libreria JQuery
Proprio all'inizio jQuery chiama automaticamente una funzione, che esporta un modulo.
Come funziona la configurazione?
Qui alcuni più dettagliati sotto-domande che potrebbero rispondere alla domanda più generale:
- Qual è l'uso della chiamata ricorsiva a
function(w)
amodule.exports
? - Qual è l'uso della variabile
noGlobal
? - Dove si trova effettivamente lo
factory
e qual è il suo tipo? - Perché l'argomento
factory
viene chiamato con un argomento e anche con due? - Qual è l'argomento
global
che si suppone debba contenere? (Vorrei che ci fosse un tipo come in C++ ...)
(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) {
un'altra domanda: da dove viene l'argomento w? – Beginner
@Beginner Quando l'oggetto globale non contiene un 'documento', restituisce una funzione che accetta un argomento, il' w'. Questo oggetto sarebbe un oggetto 'finestra' con un' documento' che può essere creato con qualcosa come jsdom. –
Questo significa che w è una variabile globale che è disponibile nel caso in cui nessun documento sia disponibile? – Beginner