Hi Mike,

You are right, I meant to say "enclosures of either kind"... ;-)

We are on the same page with this. You don't *have* to do with unless
noConflict() is on, but it's "safer" to do it anyway, specially when you
share your code (ie.: plugin developers). But like yourself, I do it all the
time to avoid accidentally messing around with global vars. It's "good
practice"...

Anyway, I'm glad to meet the guy who started it all :-)
I've actually ellaborated on the idea and come up with my own version:
;if(jQuery) (function($){
 ...
})(jQuery);

...which:
- encloses local vars
- silently ignores the code if jQuery is unavailable
- avoid colisions when files are merged (; at the start)

Cheers,
Diego A.

2008/7/4 Michael Geary <[EMAIL PROTECTED]>:

>  Sorry, Diego, but you may have made things *more* confusing! :-)
>
> If you change "enclosures of *that* kind" to "enclosures of *either* kind"
> it will probably be more clear.
>
> As you said, there isn't any real reason for plugin developers to prefer
> one of these two forms over the other. It's just a matter of historical
> accident. I posted a message in this group a couple of years ago that
> mentioned the (function($){...})(jQuery); approach, and everybody liked it
> and started using it in their plugins. It could have just as easily been the
> (function(){var $=jQuery;...})(); version.
>
> Regarding the comment that there may not be any need to do this other than
> the noConflict thing - actually I do it all the time just to get a local
> scope for some variables. I don't want to put variables into the global
> namespace when I can avoid it.
>
> -Mike
>
>  ------------------------------
> *From:* jquery-en@googlegroups.com [mailto:[EMAIL PROTECTED] *On
> Behalf Of *Diego A.
> *Sent:* Thursday, July 03, 2008 6:44 PM
> *To:* jquery-en@googlegroups.com
> *Subject:* [jQuery] Re: Why (function($){ ...})(jQuery) instead of
> (function(){var $ = jQuery; ...})()?
>
> Hi there,
>
> No, there's no difference. They both have the exact same effect.
>
> But there's a reason why plugin developers use enclosures of that kind.
> They do it to make sure they can use $ instead of jQuery even when the user
> is using jQuery.noConflict().
>
> Perhaps you want to isolate your own variables within an anonymous
> function, but unless you're using jQuery.noConflict(), you don't actually
> need to do this at all.
>
> I hope that clears up any confusion...
>
> Diego
>
> 2008/7/3 jfine <[EMAIL PROTECTED]>:
>
>>
>>
>> On 3 Jul, 21:35, "Michael Geary" <[EMAIL PROTECTED]> wrote:
>> > Yes, either one will do exactly the same thing. It's purely a matter of
>> > taste.
>>
>> Thank you both for this.  I think Michael right, but perhaps Diego
>> would like to response - he thinks there is a difference.
>>
>> Thank you also Michael for the semicolon warning.  I'm now using js2-
>> mode.el with Emacs.  It warns me about such errors.
>>
>> --
>> Jonathan
>>
>
>
>
> --
> Cheers,
> Diego A.
>
>


-- 
Cheers,
Diego A.

Reply via email to