Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What does this do? (function($){ ... })(jQuery);

 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes I see people code javascript like this:


What kind of purpose does this wrapper do? Thanks!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It ensures that within the body of the outer function, that the $ alias refers to the jQuery object.

This is important in environments where other libraries that use the $ name (Prototype, etc) may be present.

It's routinely used by anyone who writes script (such as plugin authors) who do not know what sort of pages their code will be used within.
 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear! This is cool.

I was using jQuery.noConflict().

This wrapper thing is better.
 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW the link that I should have studied more carefully: http://docs.jquery.com/Using_jQuery_with_Other_Libraries
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bruce Jin wrote:I was using jQuery.noConflict().

They're actually complementary.

The noConflict() method "gives up" the $ back to Prototype (or whatever), and the wrapper let's you use $ as a jQuery within the body of the wrapped function.

For heterogenous pages, it's common to see both used.
 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear.

It seems to me that if I use the wrapper I do not need noConflict() method. I can always wrap my jQuery code and use it any where in the page. Why both methods are used sometimes?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just using the wrapper won't let Prototype use the $ function. It's only important in pages that use more than one library, or that need to use the $ for some other purpose than jQuery for some reason.

As I said, plugin authors use the wrapper because they have no idea which pages their script will be used upon. So it's a safe way to make sure that no one steps on the $.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I personally prefer using jQuery over the $. I had to use conflict mode since a homegrown library uses it, even though we are phasing out the home grown library, I do not think I will switch back. I think it is easier to read and understand.

Eric
 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Now it makes sense.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic