• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes I see people code javascript like this:


What kind of purpose does this wrapper do? Thanks!
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 $.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Now it makes sense.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic