aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes What does this do? (function($){	 ... })(jQuery); Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "What does this do? (function($){	 ... })(jQuery);" Watch "What does this do? (function($){	 ... })(jQuery);" New topic
Author

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

Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 671
Sometimes I see people code javascript like this:


What kind of purpose does this wrapper do? Thanks!


BJ - SCJP and SCWCD
We love Java programming. It is contagious, very cool, and lot of fun. - Peter Coad, Java Design

Crazy Bikes created by m-Power
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 671
Thanks Bear! This is cool.

I was using jQuery.noConflict().

This wrapper thing is better.
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 671
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

Joined: Jan 10, 2002
Posts: 61606
    
  67

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

Joined: Sep 20, 2001
Posts: 671
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

Joined: Jan 10, 2002
Posts: 61606
    
  67

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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Sep 20, 2001
Posts: 671
Thanks. Now it makes sense.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What does this do? (function($){ ... })(jQuery);