Executing function expressions immediately (immediately-invoked functions expression)
Iife
We know that in JavaScript (ES5), there is no concept of block-level scope. See an example
for (var i = 0; i < 5; i++) {}console.log (i); // 5
Because there is no concept of block-level scope, the I variable declared in the For loop is actually a global variable that can be accessed in the global environment.
Block-level scopes, which can also be called private scopes. That is, it is defined only in the statement block of the For loop, and once the loop is over, the variable i is destroyed. In ES5, we mainly use anonymous functions to block-level scopes.
Syntax for an anonymous function that is used as a block-level scope (private scope):
(function() { /// This is a block-level (private) Scope }) ()
The above code defines and immediately invokes an anonymous function. Include a function declaration in a pair of parentheses to indicate that it is actually a function expression. See how this writing is produced.
var function () { console.log (123);}; A (); // 123
We assign an anonymous function to a global variable A, and then call this function
Of course, you can call a function like this
var function () { console.log (123);}; A (); // 123
Since this method can execute a function immediately, it means that assigning an anonymous function to a variable is completely meaningless.
If this is achieved:
function () { console.log (123);} (); // syntaxerror:function statement requires a name
This code causes a syntax error because JavaScript will function
keyword is used as the start of a function declaration, and the function declaration is not followed by parentheses (an anonymous function is one of the function declarations). However, the function expression can be followed by parentheses. To convert a function declaration into a function expression, simply add a pair of parentheses to it as follows.
// function name doesn't make sense, so use anonymous functions // First parenthesis: Converts an anonymous function to a function expression. // Second parenthesis: Executes the anonymous function immediately (function() { console.log (123);}) () /// Of course, you can also give a function name, but the function name is meaningless here, because the entire function is called immediately when it executes. (function Keith () { console.log (123);}) ()
Summarize
After understanding the above content, summarize the iife priority:
1. Create block-level (private) scopes, avoid adding variables and functions to the global scope, and therefore avoid naming conflicts for global variables and functions in multi-person development
Any variables and functions defined in 2.IIFE will be destroyed at the end of execution. This approach reduces the memory problems that are used by closures because there are no references to anonymous functions. As soon as the function is executed, its scope chain can be destroyed immediately.
Application
Immediate execution function expressions are mostly related to closures, so look at these two articles
Portal:
In-depth understanding of JavaScript function parameters and closures (i.)
In-depth understanding of JavaScript closures (ii)
What is Iife