I tried googleing, but Google doesn't seem to care about parentheses...
-
4http://stackoverflow.com/questions/3458283/in-javascript-what-does-this-syntax-mean and http://stackoverflow.com/questions/2309614/what-does-this-javascript-jquery-syntax-mean SO has its own search. I found these answer using the title of your question. – user113716 Nov 12 '10 at 17:23
-
nice you asked this question , we all use this and don't know what it does. I got it clarfied now. – kobe Nov 12 '10 at 17:34
-
1@gov - This question is asked all the time. Here's another: http://stackoverflow.com/questions/2937227/jquery-what-does-function-jquery-mean and another: http://stackoverflow.com/questions/2976089/can-someone-explain-what-the-syntax-means-when-defining-a-jquery-plugin and another: http://stackoverflow.com/questions/2464635/what-does-function-jquery-do-mean – user113716 Nov 12 '10 at 17:47
3 Answers
If you see this:
(function($) {
// ...code using $...
})(jQuery);
It's doing two things:
- Defining an anonymous function that uses
$
as its reference to jQuery. - Calling it, passing in
jQuery
.
You could do it like this:
function foo($) {
// ...code using $...
}
foo(jQuery);
...but that creates an unnecessary symbol.
All of this is because jQuery has the symbol jQuery
and the symbol $
, but it's not uncommon for people to use jQuery.noConflict()
to tell jQuery to return $
back to whatever it was when jQuery loaded, because a couple of other popular libraries (Prototype and MooTools, to name two) use $
and this lets someone use those libraries and jQuery together. But you can still use $
within your function, because the argument shadows whatever that symbol means outside the function.

- 1,031,962
- 187
- 1,923
- 1,875
-
why use an anonymous function? If it's anonymous and not reuseable, why make it a function? why not use expressions? – polyhedron Nov 12 '10 at 17:24
-
1@polyhedron - This function is meant to run only once, so no need for a name. The point T.J. is making is that `$` becomes a local variable inside the function, where it references the global `jQuery` variable. If you have `$` as a global variable, there will be a conflict if another library is using the same global variable name. This way it is private, and therefore protected. – user113716 Nov 12 '10 at 17:33
-
1@polyhedron: In JavaScript, *only* functions can create new scope, so you have to use a function if you want to create a private variable. – T.J. Crowder Nov 12 '10 at 17:42
It basically automatically invokes the anonymous/lambda function defined and supplies the jQuery
reference to it.
Pretty much the same as functionCall(jQuery)
except you define it and invoke it in the same line/expression.

- 183,342
- 71
- 393
- 434
Usually these are of the form...
(function($) {
// do something with $
})(jQuery);
Which means that the function is actually doing something with jQuery
(it defines function(a) {...}
and then passes jQuery
in as a
), but it wants to be able to use $
instead jQuery
inside the function.
This allows things like $(this)
and so on even if jQuery is invoked in No-Conflict mode (and thus $
wouldn't refer to jQuery normally).

- 507,862
- 82
- 626
- 550