Encapsulating jQuery Object
Recently I posted a little snippet of code that I was having an issue of setting element focus. One of the responses was that my jQuery Encapsulation code was incorrect. Instead of just making the change, I did some research and found "REFERENCING MAGIC - SHORTCUTS FOR JQUERY".
http://docs.jquery.com/Using_jQuery_with_Other_Libraries
On this page the jQuery Document outlines 4 different ways for encapulating jQuery:
Referencing Magic - Shortcuts for jQuery
If you don't like typing the full "jQuery" all the time, there are some alternative shortcuts:
* Reassign jQuery to another shortcut
o var $j = jQuery;
o (This might be the best approach if you wish to use different libraries) ccnp
* Use the following technique, which allows you to use $ inside of a block of code without permanently overwriting $:
o (function($) { /* some code that uses $ */ })(jQuery)
o Note: If you use this technique, you will not be able to use Prototype methods inside this capsuled function that expect $ to be Prototype's $, so you're making a choice to use only jQuery in that block.
* Use the argument to the jQuery(document).ready(function($) {})
DOM ready event:
*
o jQuery(function($) { /* some code that uses $ */ });
o Note: Again, inside that block you can't use Prototype methods.
I normally always use jQuery(function($) { /* some code that uses $ */ }); because I think it's easy for other coders to read what's happening compared to (function($) { /* some code that uses $ */ })(jQuery) .
Obviously, my use of the jQuery(function($){}) is for personal preference of readability, however the other developer insisted it was wrong to code it this way.
My questions...
Is it wrong?
What makes (function($){})(jQuery) better?