1. the traditional method definition (statement) is implemented as follows:
function foo () {...} This is a definition; the definition simply lets the interpreter know it exists, but it does not run.
Foo (); This is the statement, and the interpreter encounters the statement that will run it.
Note:(1) Traditional methods are cumbersome, definition (declaration) and execution separate, (2) Traditional methods directly pollute the global namespace.
2. the currently more popular approach is defined as follows:
function foo () {...}) ();
Or
(function foo () {...} ());
3. In addition, the above writing, there are many other writing, such as:
!function foo () {...} ();
Or
+function foo () {...} ();
That's all you can do. That is, +function () {} or!function foo () {} is equal to (function () {}).
* Summary
A. (function () {}) (); is to parse the function as an expression and then perform the parsed function, which is equivalent to var a = function () {}; A (); A gets the function.
B. (function () {} ()); is to execute the function expression and execution as a statement directly, the equivalent of var a = function () {} (); A what gets is the result.
The end result is the same for both of these methods.
C. General use (function () {}) has a role in the avoidance of global variables;
Note: The so-called do not pollute the global namespace, because the two definitions above create a new function scope, your real business code is encapsulated in it, naturally will not touch the global object.
If you need a global object, pass the parameters as follows:
void function (Global) {
//Here, Global is the Universal Object
} (this) //In the browser, this is the Window object