js immediately call function IIFE (Immediately Invoked Function Expression) [in turn] JS (function () {...}) () function is executed immediately

Original link: https://www.cnblogs.com/ming-os9/p/8891300.html

In JS (function () {...}) () function is executed immediately

 
1 (function(){...})()
3 (function(){...}())

These are two common wording js execution of the function immediately.

 

 

basic concepts:

Function declaration: function fname () {...}; declare a function using the function key, and then specify a function name.

Function expression: var fname = function () {...}; using the function keyword to declare a function, but not to the function name, and finally given an anonymous function to a variable.

Anonymous function: function () {}; use the function keyword to declare a function, but not to the function name. (Anonymous functions also belong to the function expression.)

(Many anonymous function role, gives a variable function is created, the event has become a given event handlers, etc. ..)

 

 

About function declaration and function expression of differences:

1: Function declaration Hoisting function declarations (who replaced the word is to enhance the function declaration), function expressions do not have this, it needs to be resolved when the current js code can call this line.

2: function expression behind the plus () immediately calls the function, function declaration can not, it can only fname () call.

 

 

example:

Copy the code
1 fName();
2 function fName () {...} // correct, enhance the function declaration, so fName () can be written before the function declaration.
3 
4 fName();
5 var fName = function () {...} // error function declaration does not have a function to enhance expression.
6 
7 var fName = function () {...} (); // correct function expression behind plus () function call immediately.
8 
9 function fName () {...} (); // error function declaration must be called by fName (). 
// This line is parsed into two parts: a function declaration function fName () {...}, 2 grouping expression (), which expression error, because there is no expression in parentheses

function () {.. .} (); // call the anonymous function can not be so, because the function () {...} is treated as a statement, the statement can not be directly () call.
Copy the code

 

By the way immediately call IIFE (Immediately Invoked Function Expression):

(Function () {...}) () and (function () {...} ()) is no difference!

The traditional definition of function is:

1 function foo () {...} // This is the definition, Declaration, just let the interpreter know of its existence, does not run
2 
3 foo (); // This is a statement, the interpreter encounters statement will run it

Why IIFE it? 1: The traditional method of long-winded. 2: traditional methods pollute the global namespace

So we write such a   function foo () {...} ( ); written okay. . No, why, because the function foo () {...} This statement is only part of the interpreter who, like you wrote the string string "function foo () {...} ", it needs to be analytic function can be used such as eval () to execute it can. So put () is placed directly behind the statement is not enough, wrong grammar!

However, we are very close distance success, just need to declare the function becomes a function expression on it. The method is very large, the most common method is to use one pair () wrapped up. (function foo () {...}  ) ();

This is equivalent to

1 var foo= function(){...};
2  foo();

Of course there are many other ways to become an expression statement:

1  !function foo(){...}(); 
2 +function foo(){...}(); 
3 void function() {...}();

When needed global object, so to pass, for example:

1 void function(global){
2 console.log ( "a's value is:" + global.aa); // can obtain the value of the global object aa
3 }(this)
1 var aa = 10;
2 (function(a){
3       console.log("hello world"+a);
4 })(aa);
1 (function(){...})()
3 (function(){...}())

These are two common wording js execution of the function immediately.

 

 

basic concepts:

Function declaration: function fname () {...}; declare a function using the function key, and then specify a function name.

Function expression: var fname = function () {...}; using the function keyword to declare a function, but not to the function name, and finally given an anonymous function to a variable.

Anonymous function: function () {}; use the function keyword to declare a function, but not to the function name. (Anonymous functions also belong to the function expression.)

(Many anonymous function role, gives a variable function is created, the event has become a given event handlers, etc. ..)

 

 

About function declaration and function expression of differences:

1: Function declaration Hoisting function declarations (who replaced the word is to enhance the function declaration), function expressions do not have this, it needs to be resolved when the current js code can call this line.

2: function expression behind the plus () immediately calls the function, function declaration can not, it can only fname () call.

 

 

example:

Copy the code
1 fName();
2 function fName () {...} // correct, enhance the function declaration, so fName () can be written before the function declaration.
3 
4 fName();
5 var fName = function () {...} // error function declaration does not have a function to enhance expression.
6 
7 var fName = function () {...} (); // correct function expression behind plus () function call immediately.
8 
9 function fName () {...} (); // error function declaration must be called by fName (). 
// This line is parsed into two parts: a function declaration function fName () {...}, 2 grouping expression (), which expression error, because there is no expression in parentheses

function () {.. .} (); // call the anonymous function can not be so, because the function () {...} is treated as a statement, the statement can not be directly () call.
Copy the code

 

By the way immediately call IIFE (Immediately Invoked Function Expression):

(Function () {...}) () and (function () {...} ()) is no difference!

The traditional definition of function is:

1 function foo () {...} // This is the definition, Declaration, just let the interpreter know of its existence, does not run
2 
3 foo (); // This is a statement, the interpreter encounters statement will run it

Why IIFE it? 1: The traditional method of long-winded. 2: traditional methods pollute the global namespace

So we write such a   function foo () {...} ( ); written okay. . No, why, because the function foo () {...} This statement is only part of the interpreter who, like you wrote the string string "function foo () {...} ", it needs to be analytic function can be used such as eval () to execute it can. So put () is placed directly behind the statement is not enough, wrong grammar!

However, we are very close distance success, just need to declare the function becomes a function expression on it. The method is very large, the most common method is to use one pair () wrapped up. (function foo () {...}  ) ();

This is equivalent to

1 var foo= function(){...};
2  foo();

Of course there are many other ways to become an expression statement:

1  !function foo(){...}(); 
2 +function foo(){...}(); 
3 void function() {...}();

When needed global object, so to pass, for example:

1 void function(global){
2 console.log ( "a's value is:" + global.aa); // can obtain the value of the global object aa
3 }(this)
1 var aa = 10;
2 (function(a){
3       console.log("hello world"+a);
4 })(aa);

Guess you like

Origin www.cnblogs.com/langren1992/p/11689595.html