start javascript code with $(function, etc

后端 未结 3 1010
说谎
说谎 2020-12-01 06:16

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:

$(funct         


        
3条回答
  •  长情又很酷
    2020-12-01 06:57

    These two:

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

    Are directly equivalent, they are both the way to start some jQuery when the document has loaded. The former is just a shorter version of the latter.

    This one:

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

    is just a scoped function with zero parameters, which is immediately called with zero parameters.

提交回复
热议问题