(Function (window, undefined) {}) (window);
Well, why would you pass window and undefined as arguments to it?
(function ($, undefined) {}) (JQuery); Similarly
Because the ECMAScript executes the JS code from inside to outside, so the global variable window or jquery object is passed in, it avoids the outer to look for, improve efficiency. Undefined in the older generation of browsers is not supported, direct use will be error, JS framework to consider compatibility, so add a formal parameter undefined.
Also, do not use window.undefined to pass to the formal parameter, it is possible window.undefined be modified by others, the best is nothing to preach, formal parameter undefined is the real undefined.
JS Code:
- var undefined = 8;
- (function (window) {
- alert (window.undefined); //8
- alert (undefined); //8
- }) (window);
With JS code:
- var undefined = 8;
- (function (window, undefined) {
- alert (window.undefined); //8
- alert (undefined); ///Here The undefined parameter is a local name undefined variable with a value of undefined
- }) (window);
Distinguish between the two ways: the first one is to find a window for each of the statements. The second is to pass the window as a parameter, and not to go to the window for each statement, it should be more efficient. So the latter, even if the outside people to define the undefined, the inside of the undefined is still unaffected. Presumably to minimize the impact of external variable definitions on the internals of the package.
Original reference from webmaster net http://www.software8.co/wzjs/JavaScript/2525.html
JS Code:
- Way One
- (function (undefined) {
- Window.property1 = ...;
- Window.property2 = ...;
- ......
- })();
- Way Two
- (function (window, undefined) {
- ... //code goes here
- }) (window);
- Mode three
- (function (undefined) {
- var tmp = window;
- Tmp.property1 = ...;
- Tmp.property2 = ...;
- ......
- })();
The efficiency of mode one is obviously the lowest, the way two and the way three should be similar. Passing the window as a parameter allows the statement inside the code to use the window directly in the argument without having to find the outermost object. If you want to set 100,000 properties for the window in the function, passing the argument in the past only needs to find the outermost object once. Without argument passing, the statement using the window will have to find the outermost object.
function ($, undefined) {}) (jquery) circumvents the user's definition of undefined, resulting in an exception to the functions (function ($, undefined) {}) (jquery) if the user var undefined =x Iaoyan, (function ($, undefined) {}) (JQuery)//Here you can pass a undefined parameter, avoid the definition above, as long as the value is not passed in, undefined will not be assigned
"Go" in jquery Source (function (window, undefined) {}) (window)