开发者

JQuery ready event and its shortcut

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 projec开发者_开发技巧ts?


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


They are equivalent


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜