Two of the most common immediate execution functions in JS
- (function () {...}) ()
- (function () {...} ())
The difference between a function declaration and a function expression is that:
- The JavaScript engine will ' function declaration promotion ' (function declaration hoisting) Functions declaration on the current execution environment (scope) when parsing JavaScript code, The function expression must wait until the JAVASCIRTP engine executes to its row to parse the function expression from the top and the next line.
- The function expression can be called immediately after parentheses, and the function declaration cannot be called only in the form of FnName ().
var fnname=function () {alert ( ' Hello World ' // The function expression is followed by parentheses, which can be called immediately when the JavaScript engine resolves here function FnName () {alert ( ' Hello World ' // function () {Console.log ( ' Hello World ' // syntax error, although the anonymous function is a function expression, but the assignment operation is not performed, // So the JavaScript engine takes the function keyword at the beginning as a declaration of functions, an error: Requires a name of the functor
To call immediately after the function body is appended with parentheses, the function must be a function expression, not a function declaration .
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.
(function(a) {Console.log (a); //firebug output 123, using the () operator}) (123); (function(a) {Console.log (a); //firebug output 1234, using the () operator} (1234)); !function(a) {Console.log (a); //firebug output 12345, use! Operator} (12345); +function(a) {Console.log (a); //firebug output 123456, using the + operator} (123456); -function(a) {Console.log (a); //firebug output 1234567, using-operator} (1234567); varfn=function(a) {Console.log (a); //firebug output 12345678, using the = operator} (12345678)
You can see the output and add it in front of the function! , +,-even a comma wait for the effect to be executed immediately after the function definition, and (),! , + 、-、 =, and so on, all function declarations are converted into function expressions , eliminating the ambiguity of the JavaScript engine recognition function expressions and function declarations, telling the JavaScript engine that this is a function expression, not a function declaration, you can add parentheses later, And immediately executes the code for the function.
Parentheses are the safest thing to do, because! , +,-et operators also operate on the return values of functions, sometimes causing unnecessary hassles.
The effect of this writing
The concept of a private scope in JavaScript, if you declare some variables in a global or local scope on a multi-person project, may be overridden by someone else accidentally with a variable of the same name, depending on the nature of the scope chain of the JavaScript function, You can use this technique to mimic a private scope, use an anonymous function as a "container," inside the container to access external variables, and the external environment to access variables inside the container, so (function () {...}) () internally defined variables do not collide with external variables, commonly known as "anonymous wrapper" or "namespace."
JS Execute function immediately