self executing function jquery vs javascript difference

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

What are the difference among -

First :-

(function () {

    var Book = \'hello\';

}());

Second:-

9条回答
  •  情话喂你
    2020-12-02 20:46

    In all cases you are doing an anonymous function. I think 1 is the same as 2. In the third case you are passing jQuery as an argument. This is done when you want to encapsulate jQuery within your function's scope.

    For instance, in your application, jQuery var could be jQuery. But within your anonymous function you may want to use it as $.

    (function ($) {
        //Here jQuery is $
        var Book = $(document.body).text();    
    
    })(jQuery);
    
    //Out of your function, you user jQuery as jQuery (in this example)
    var Book = jQuery(document.body).text();
    

提交回复
热议问题