Tôi đoán có ý nghĩa khi bắt đầu, bằng cách nhận ra rằng $ = jQuery
. Mục đích của nó dưới đây khi đọc về các không gian tên trong các hàm ẩn danh sẽ có ý nghĩa hơn. Nhưng về bản chất, bạn có thể sử dụng một trong hai. Người ta sẽ sử dụng jQuery()
thay vì $()
nếu họ đang sử dụng nhiều thư viện và muốn sử dụng $
để người khác sử dụng.
$(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()
Đưa $
trong ngoặc đảm bảo jQuery $ alias (bạn có thể được an toàn nó luôn luôn có nghĩa jQuery theo cách này).
$(function ($) { /* code here : $ always means jQuery now */ });
Cuối cùng bạn có một IIFE (Immidiately-Được triệu gọi hàm Expression) - 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));
Các $ ở đầu (với nó phù hợp với jQuery ở phía dưới) biểu thị rằng $ (ký hiệu đô la) là viết tắt của jQuery trong phạm vi của các namepsace. Điều này được thực hiện để đảm bảo rằng các thư viện khác không xung đột với những gì nhà phát triển dự định/muốn $ được sử dụng cùng.
(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
Bây giờ nếu chúng ta bên ngoài namespace, chúng ta có thể xem như thế nào những phương pháp công cộng/nội bộ và các biến được thực hiện:
// 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());
Ví dụ thứ ba là ** không ** tương đương với những người khác. – PPvG
Xem thêm [Ký hiệu đô la trước khi tự khai báo chức năng ẩn danh trong JavaScript?] (Https://stackoverflow.com/questions/7614574/dollar-sign-before-self-declaring-anonymous-function-in-javascript] – Bergi