What is the purpose of this? (function ($) { //function code here })(jQuery);

前端 未结 4 1112
遇见更好的自我
遇见更好的自我 2020-12-01 21:13

I am debugging someone else\'s JavaScript code and a majority of the code is wrapped like this:

(function ($) {
    //majority of code here...
})(jQuery);
         


        
4条回答
  •  青春惊慌失措
    2020-12-01 21:45

    This is useful when you want / need to use jQuery.noConflict(), and the global name $ isn't an alias for jQuery. The code you posted lets you use the shorter $ to mean jQuery inside the anonymous function only, without $ needing to be a global.

提交回复
热议问题