views:

60

answers:

4

Hello,

In most of the videos, I see expert JQuery developers writing complete code for the ready event eg:

$(document).ready(function(){
  //.....
});

rather than its shortcut:

$(function(){
  //.....
});

Is there any particular down side to using shortcut method?

Edit:

Jquery documentation says:

shortcut not recommended

That's my question, why is it not recommended? Should we ever use it in our projects?

+1  A: 

They are equivalent

Jeremy
Actually, the doc is not up to date, see http://jquery14.com/day-01/jquery-14#backwards
David V.
@Jer: I forgot to mention that too. Why jquery documentation says shortcut way is not recommended, any particular reason for that, should we use it assuming it will never ever create problems?
Sarfraz
@David, you're misreading the documentation. The change was only to the no-argument call, not the call that passes in a "ready" handler function.
Pointy
The doc mentions $().ready( callback ), and this no longer works. My statements that the doc is outdated stands.
David V.
You're wrong, @David. That does work. Here's a demo page: http://gutfullofbeer.net/ready.html
Pointy
Well my argument was from this sentence in the link I provided : jQuery() (with no arguments) no longer converts to jQuery(document).I stand corrected, and I will go hide in in shame in a corner now.
David V.
@David spoken like a gentleman - my shame corner is warm most of the time :-)
Pointy
@Pointy, +1 for the domain name of your demo page :-D
David V.
+2  A: 

Good question... sometimes these shortcuts breed as much confusion as convenience.

Per the jQuery docs:

All three of the following syntaxes are equivalent:

* $(document).ready(handler)
* $().ready(handler) (this is not recommended)
* $(handler)

EDIT

$().ready(handler) is not recommended in the documentation and, in the latest version of jQuery, will not work.

In older versions of jQuery, $(document) and $() were equivalent. Per the jQuery site, this is no longer the case

Dancrumb
The doc seems outdated, see http://jquery14.com/day-01/jquery-14#backwards
David V.
@Dancrumb: I forgot to mention that too. Why jquery documentation says shortcut way is not recommended, any particular reason for that, should we use it assuming it will never ever create problems?
Sarfraz
Dancrumb, please edit anything to your reply so I can remove the negative vote (I sincerely thought it was wrong)
David V.
Sorry @David. Looks like you'll have to live with this guilt forever :P
Anurag
@David Edit made, thanks for pointing that out
Dancrumb
+1  A: 

The shortcut ( to $().ready( callback ) ) is no longer valid as of jQuery 1.4

Edit: starting with jQuery 1.4, $() is the empty jQuery object.

This is explained here : http://jquery14.com/day-01/jquery-14#backwards

David V.
+1: thanks for sharing that, did not know that :)
Sarfraz
That is absolutely wrong. The "shortcut" most certainly does NOT pass nothing to the function - it passes a function.
Pointy
I was thinking about $().ready(....) ...
David V.
The jQuery function's prototype object has a "ready" function, so $().ready(...) is effectively the same as $(document).ready(...).
Pointy
Ok, so I misunderstood the link I posted and would (sincerely) greatly appreciate that you explain for me this statement : jQuery() (with no arguments) no longer converts to jQuery(document). ---- I had assumed that $().ready worked as $(document).ready because $() was the same as $(document).
David V.
A: 

I think all answers have already clarified that $().ready(function) is not recommended.

$(function) is the other shortcut and all with all shortcuts its upto you to decide whether the gain of writing 16 lesser character outweighs the loss of clarity for your purposes.

Anurag
All those clarifications are wrong, and in any case *that was not the "shortcut" mentioned in the original question!*
Pointy
I meant `$().ready(function)` is not recommended because that's where all the discussion seemed to be heading. Internally jQuery ignores the passed-in document object altogether in the ready() call , so as a leaked abstraction `$().ready()` works just like `$(document).ready()`. In any case, I have tried to answer the question about the shortcut in OPs question too.
Anurag