start javascript code with $(function, etc

Posted by YAmikep on Stack Overflow See other posts from Stack Overflow or by YAmikep
Published on 2012-08-17T15:38:23Z Indexed on 2014/06/04 15:25 UTC
Read the original article Hit count: 111

Filed under:
|
|

I am studying Backbone and the todo example apps from http://todomvc.com/ I have noticed there are 3 severals ways of starting the code in the files:

$(function() {
 // code here
});

$(function( $ ) {
 // code here
});

(function() {
 // code here
}());

I do not understand the differences and when I should use one over the other.

I also saw some people using this to start their code:

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

From what I have seen, this is the full way of writing it right?

In a more general way, should I always include my javascript code into something like that in each files?

Thanks for your advice.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery