How does this JavaScript/jQuery syntax work: (function( window, undefined ) { })(window)?

后端 未结 5 1347
暗喜
暗喜 2020-11-22 00:30

Have you ever taken a look under the hood at the jQuery 1.4 source code and noticed how it\'s encapsulated in the following way:

(function(          


        
5条回答
  •  有刺的猬
    2020-11-22 01:15

    The undefined is a normal variable and can be changed simply with undefined = "new value";. So jQuery creates a local "undefined" variable that is REALLY undefined.

    The window variable is made local for performance reasons. Because when JavaScript looks up a variable, it first goes through the local variables until it finds the variable name. When it's not found, JavaScript goes through the next scope etc. until it filters through the global variables. So if the window variable is made local, JavaScript can look it up quicker. Further information: Speed Up Your JavaScript - Nicholas C. Zakas

提交回复
热议问题