self executing function jquery vs javascript difference

后端 未结 9 790
长发绾君心
长发绾君心 2020-12-02 19:59

What are the difference among -

First :-

(function () {

    var Book = \'hello\';

}());

Second:-

9条回答
  •  一向
    一向 (楼主)
    2020-12-02 20:50

    This is called a closure to avoid conflicts with other libraries such as mootools which are using $. This way you can ensure to use $ in that function with passing jQuery as a param.

    (function ($) {
       $(function () { // Here in this block you can use '$' in place of jQuery
        .......
       });
    })(jQuery); //<----passing jquery to avoid any conflict with other libraries.
    

提交回复
热议问题