Why write code Jquery like this

前端 未结 4 787
春和景丽
春和景丽 2020-12-10 15:10

Why write code Jquery like this?

(function ($) {
    $(function () {
     .......
    });
})(jQuery);
4条回答
  •  攒了一身酷
    2020-12-10 15:27

    (function ($) {
      //
    })(jQuery);
    

    This type of Module pattern is very used out there. It invokes itself passing a reference to jQuery providing faster access to the variable, as it now lives in the scope of the function, and it also prevents global pollution.

    The second one:

    $(function () {
        .......
    });
    

    Runs the anonymous function once the DOM is loaded, that you make sure that everything is ready before executing any code.

提交回复
热议问题