jQuery question: what does it really mean?

后端 未结 5 1346
北海茫月
北海茫月 2020-12-01 07:03
(function($, window, undefined){
  ... jquery code... 
})(jQuery, window);

What does it really mean? Does it also mean $(document).ready()

5条回答
  •  孤城傲影
    2020-12-01 07:38

    Like that it doesn't mean much at all (in addition a closing ) is missing). What you are probably looking at is something similar to this:

    (function($, window){
        // code
    } )($, window);
    

    This puts the code inside a new scope, so you can for example define variables without messing with the outside scope.

    It also allows that you pass different things to the function, without having to change the code. For example if you use different JavaScript frameworks, and $ is not bound to jQuery, you can pass the alternative variable for jQuery instead, while you are still using the $ to refer to jQuery internally. Similar to that you could also pass a different window instance (for example from a popup).

提交回复
热议问题