1 var aa=function() {} (); 2 3 var bb= (function() {}) ();
Today is asked this question, this section JS has the difference.
To summarize, the two functions are meant to be executed immediately. But the difference is the order of execution, the AA function first knows the function and then the parentheses.
The BB function is an anonymous function that executes as a function and then executes. BB functions are commonly used in JS packet development.
Self-executing anonymous function:
- Common formats: (function () {/* code */}) ();
- Explanation: The first pair of parentheses of the enclosing function (function () {}) returns an unnamed function to the script, followed by a pair of empty parentheses that immediately execute the returned unnamed function, with the parameters of the anonymous function in parentheses.
- Role: You can use it to create a namespace, as long as you write all of your own code in this special function wrapper, then the external can not access, unless you allow (the variable is preceded by the window, so that the function or variable becomes global). The code for each JavaScript library is basically this form of organization.
To summarize, the function of execution is mostly anonymous and automatic , and the code is already running when it is interpreted.
Other wording
(/* Code */} ());! /* Code */} (); ~/* Code/} ();-*/* Code */} (); +/ * Code */} ();
Ext.: http://www.cnblogs.com/TomXu/archive/2011/12/31/2289423.html
JS () () problem