Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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(){});



I'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.


For 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(){});


On 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.


That 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.


I think the point is that they should learn enough jQuery before maintaining jQuery code.


Or even better:

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


You 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/


Even 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.


This is my preferred pattern:

jQuery(function($){

});

It would avoid noConflict type problems.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: