8 ms·
I know this is off topic, but there's another reason it slows you down - too many keystrokes. I prefer $(function(){}); to $(document).ready(function(){});
by nphase 16y ago
I know this is off topic, but there's another reason it slows you down - too many keystrokes.
I prefer
$(function(){});
to
$(document).ready(function(){});
- Encosia 16y agoI'm torn when it comes to that. The readability of $(document).ready() is better. Especially for later devs who may not be familiar with jQuery.
- billybob 16y agoFor devs not familiar with jQuery (and on the off-chance that you'll need no-conflicts mode at some point), you might even avoid the dollar sign. jQuery(document).ready(function(){});
- IgorPartola 16y agoOr even better: jQuery(document).ready(function($){});
- Encosia 16y agoYou can also use the module pattern[1] to isolate code from tampering or conflicts: (function($, window, undefined) { $(document).ready({ // etc. }); }(jQuery, window); [1] http://www.yuiblog.com/blog/2007/06/12/module-pattern/ http://www.yuiblog.com/blog/2007/06/12/module-pattern/
- JustinSeriously 16y agoOn the other hand, if a dev is that unfamiliar with jQuery code, they shouldn't maintaining jQuery code. Using $(function(){}); serves as a nice warning to this effect.
- rue 16y agoThat is an awful argument. Can you imagine what software would look like if people took that approach with everything? Development would have ground to a halt decades ago.
- ganjianwei 16y agoI think the point is that they should learn enough jQuery before maintaining jQuery code.
- chc 16y agoEven further off-topic, I really like the succinctness of the former in CoffeeScript: $(function () { $('#index').append($('article p:first-child')); }); becomes: $ -> $('#index').append $('article p:first-child') Not exactly noob-friendly, but very concise.
- Smuggler 16y agoThis is my preferred pattern: jQuery(function($){ }); It would avoid noConflict type problems.