Basic Javascript knowledge in Jquery source code (2), jqueryjavascript
In the previous article, jquery source code is called anonymous function self-execution.
(Function (window, undefined) {// code}) (window );
The function defines two parameters window and undefined. The window object is passed in by the execution function.
Parameter window
Because of the relationship between the scope chain of the variable object, we know that even if the window object is not passed in, the function can also access it. Therefore, there are two main Meanings of the input window object:
1. Improve Performance
According to the scope chain order of the variable object, the function first searches for the window variable internally, and the variable initially contained in the function is the arguments object, that is, the parameter variable, at the same time, the amount of code inside the function is relatively large. If it is not found, it will continue to look for the include (external) environment, which is the window object in the global environment.
The window object is passed in directly, which reduces the query time and improves the efficiency.
2. Convenient Compression
(Function (e, undefined) {// code}) (window );
View the JQ source code of the compressed version, and see that all windows in the anonymous function are replaced with e, which reduces the number of characters and further reduces the file size.
If this parameter is not used, the window cannot be changed to another identifier, so that the access is not a window object in the external environment.
Parameter undefined
The window object in the global environment has an attribute undefined. Its value is "undefined" and cannot be changed.
However, this is not the case in some browsers, such as ie7 and ie8. You can modify the value.
Therefore, to prevent undefined values from being accidentally modified, an undefined parameter is defined and is not passed during execution.
Now, this article is here.