Passing the window and undefined to the immediately called anonymous function. What for?

Possible duplicate:
why do we have to go through a window and undefined into this jquery plugin?

I saw jQuery source code:

(function(window, undefined){ ... }(window)) 

I understand why it is useful to include undefined if someone where you need to change "undefined" before. But the window cannot be resized. As far as I know, it does not even need to be used, right? How can this be helpful?

+8
javascript jquery
source share
2 answers

Micro optimization.

Having window as a local variable is slightly faster than a global variable.

It also reduces better. Now we can minimize the parameter of the function w and use w.setTimeout , etc. Instead of window.setTimeout .

Less bytes = better

+16
source share

Not only what Raynos published, but also protects your code from people who do something similar in other libraries that may be on your page, or access the page in another way:

 undefined = true; 

In other words, it protects you from other people doing stupid things, such as overriding common global variables or objects.

+9
source share

All Articles