Suppongo che abbia senso iniziare, realizzando che $ = jQuery
. Lo scopo di cui in basso durante la lettura di spazi dei nomi all'interno di funzioni anonime avrà più senso. Ma in sostanza, puoi usare entrambi. Uno userebbe jQuery()
invece di $()
se stavano usando più librerie e voleva che lo $
fosse usato dall'altro.
$(document).ready(function(){
// Here we have jQuery(document) firing off the ready event
// which executes once the DOM has been created in
// order to ensure that elements you are trying to manipulate exist.
});
$(function() {
// Short-hand version of $(document).ready(function() { });
});
More information on Document.ready()
Mettere il $
entro la parentesi assicura la jQuery $ alias (si può essere sicuri che significa sempre jQuery in questo modo).
$(function ($) { /* code here : $ always means jQuery now */ });
Infine si dispone di un IIFE (diementicato-Invocato espressione di una funzione) - IIFE explanation
(function (myNameSpace, $) {
// This is an anonymous function - it is ran instantly
// Usually used for namespaces/etc
// This creates a scope/wrapper/closure around everything inside of it
}(window.myNameSpace, jQuery));
Il $ in alto (con la sua corrispondenza jQuery sul fondo) significare che il $ (simbolo del dollaro) sta per jQuery nell'ambito di namepsace. Questo è fatto per assicurare che le altre librerie non entrino in collisione con ciò che lo sviluppatore intende/vuole usare con $.
(function (myNameSpace, $) {
// Now because of all of this scope/wrapper/closure awesome...
// you can create -INTERNAL- variables (sort of like Private variables from other langs)
// this variable cannot be accessed outside the namespace unless it is returned/exposed
var internalVariable = '123'; // Internal
// Even Internal functions!
function privateFunction() {
console.log('this is private!');
}
// --------------------------------------------------------
// Public -method- of nameSpace exposing a private variable
// Notice we're using the myNameSpace object we exposed at the top/bottom
myNameSpace.nameSpaceMethod = function() {
privateFunction(); // we can call the private function only inside of the namespace
return internalVariable; // now we could get this variable
};
}(window.myNameSpace, jQuery)); // notice these mirror the above arguments in the anon function
More information on anonymous functions
Ora, se siamo al di fuori dello spazio dei nomi, possiamo vedere come questi metodi pubblici/interni e le variabili vengono effettuate:
// This will come up undefined
alert(internalVariable);
// This will trigger a -method- of the myNameSpace namespace - and alert "123"
// Showcasing how we access a Public method - which itself has access to the internal variable
// and exposes it to us!
alert(myNameSpace.nameSpaceMethod());
Il terzo esempio è ** non ** equivalente agli altri. – PPvG
Vedere anche [Segno di dollaro prima di auto dichiarare la funzione anonima in JavaScript?] (Https://stackoverflow.com/questions/7614574/dollar-sign-before-self-declaring-anonymous-function-in-javascript] – Bergi