1

For the function (function($){})(), I've seen it with the word jQuery in it, why is that?

I have read this page Javascript: why does jQuery do this: (function(){ ...});, and how does it work? but it didnt tell about an example like this:

(function($){})(jQuery);

What does that jquery in the parenthesis do? If the parenthesis by itself already self calls the function then why do we need that jQuery in there?

Community
  • 1
  • 1
anthonypliu
  • 11,459
  • 25
  • 85
  • 149

1 Answers1

4

Is a simple function invocation, the jQuery object is sent as an argument to the anonymous function, e.g.:

(function (foo) {
  alert(foo); // alerts "hello"
})("hello");

It's a common pattern to define plugins, basically permits you to reference the jQuery object as $ in the scope of the anonymous function, even if jQuery is running in noConflict mode.

Christian C. Salvadó
  • 723,813
  • 173
  • 899
  • 828
  • This is indeed useful if you use mixed libraries, such as prototype.js, which also defines the $ variable – Chubas Jun 22 '10 at 04:48