admin管理员组文章数量:1290230
Recently I was reading someone else's code, and came across this:
// Semicolon (;) to ensure closing of earlier scripting
// Encapsulation
// $ is assigned to jQuery
;(function($) {
// DOM Ready
$(function() {
...
});
})(jQuery);
I understand the point of the leading ;, And I understand that $(function() { is the same as document ready, but what is the point of adding function($)?
I understand it's a closure, but since this is always being called at the global scope, it seems like you don't need to bother with it. The $(function() { will use the same global object either way, no?
Is it to safeguard against something, or is it a best practice for another reason?
Recently I was reading someone else's code, and came across this:
// Semicolon (;) to ensure closing of earlier scripting
// Encapsulation
// $ is assigned to jQuery
;(function($) {
// DOM Ready
$(function() {
...
});
})(jQuery);
I understand the point of the leading ;, And I understand that $(function() { is the same as document ready, but what is the point of adding function($)?
I understand it's a closure, but since this is always being called at the global scope, it seems like you don't need to bother with it. The $(function() { will use the same global object either way, no?
Is it to safeguard against something, or is it a best practice for another reason?
Share Improve this question asked Oct 12, 2012 at 14:34 nycyniknycynik 7,5418 gold badges64 silver badges88 bronze badges2 Answers
Reset to default 14It's a mon structure for a jQuery plugin. It safeguards against the $
identifier having been overwritten and used for something else. Inside the anonymous function, $
is always going to refer to jQuery.
Example:
$ = "oh no";
$(function() { //Big problem!
//DOM ready
});
By introducing a new scope, you can ensure that $
refers to what you expect it to:
$ = "oh no";
(function($) { //New scope, $ is redeclared and jQuery is assigned to it
$(function() { //No problem!
//DOM ready
});
}(jQuery));
The main reasoning behind this is that numerous other JavaScript libraries use $
as an identifier (e.g. PrototypeJS). If you wanted to use both Prototype and jQuery, you need to let Prototype have its $
identifier, but you probably don't want to write out jQuery
every time you want to call a jQuery method. By introducing a new scope you allow jQuery to have its $
back in that execution context.
The code sample you've provided is an example of a Self-Invoking Function:
(function(){
// some code…
})();
The first set of parentheses defines a function: (an anonymous function wrapped in parentheses)
(function() {})
That defines the anonymous function. On its own, it doesn't do anything. But if you add a set of parentheses ()
after the definition, it's the same as the parentheses used to call a function.
Try this out:
(function(message) {
alert(message);
})("Hello World");
That creates a function which accepts a parameter, and displays an alert box containing the provided value. Then, it immediately calls that function with a parameter of "Hello World".
In your example, a self-invoking function is defined. It accepts a parameter, which is named $
. Then, the function is immediately called, with a reference to jQuery
being passed in as the argument.
This is mon if you want jQuery to operate in noConflict()
mode (which removes the global reference to $
).
In noConflict()
mode, you can still access jQuery via the jQuery
global variable, but most people would rather use $
, so this self-calling function accepts the global jQuery
variable as a parameter named $
in the scope of the function, which leaves you free to use the $
shortcut within the self-invoking function while having jQuery operate in noConflict() mode to avoid clashes with other libraries that use $
in the global scope.
Hope this answers your question!
本文标签: javascriptCan someone explain what function() does in jQueryStack Overflow
版权声明:本文标题:javascript - Can someone explain what function($) does in jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741495019a2381798.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论