After defining a function in JavaScript, declare a function and immediately execute (call) the function. Such a function is immediately executed function expression (IIFE), also known as immediate function.
Typical style:
(function(){ //Execute statement //...... })()
It can also be:
(function () { //Execute statement //...... }())
Recommendation is the first way of writing, because it is clear and not easy to produce ambiguity.
Some habitually add;, For example; (function(){})();, This is to avoid the wrong habitual writing (there is no;) at the end of the previous sentence.
If there is no reference to external variables in the function, that is, no closure exists, the function will be garbage collected after execution.
Here are some examples:
(function func(){ console.log('B')})();//Output B console.log(!!function func(){ return 'C'; }());//Output true console.log(!(function(){ return true})());//Output false console.log(1 + function func(){ return 1; }());//Output 2 -(function func(){ console.log('D');})();//Output D
In the JavaScript language, to understand the immediate execution function, first understand the function of ().
() can be evaluated.
So what's inside the parentheses is the expression.
For example, you can write:
a=(1+2);
console.log(a);// Output 3
But write a=(1+2;) An error is reported because the statement in parentheses is not an expression.
Another example:
function a(){console.log('1')}();
This is an error message, because the JavaScript interpreter will interpret anything that starts with function with a statement.
(function a(){console.log('1')}());
This can be output correctly, because wrapped in (), the JavaScript interpreter will think that what is inside is an executable expression.
() can also be an execution operator.
My understanding is that {} corresponds to the execution () one by one. I think it is to peel onions, {} is to seal a layer of onion skin, and () is to peel a layer of onion skin.
var K=function a(){ return function b(){ return function c(){ return function d(){ return 'JavaScript'; } } } }; console.log(K()()()()); //Output JavaScript
In the JavaScript language, () is also a magical existence. Take a look at the following code, but the location of () is different, resulting in different results.
var a=( function(){ var count=0; return function (){ count=count+1; return count; } })(); console.log(typeof a); //Output function console.log(a());//Output 1 console.log(a());//Output 2 console.log(a());//Output 3
The above example uses the combination of closed packets to achieve a specific effect.
The following is just to change the position of ():
var a=( function(){ var count=0; return function (){ count=count+1; return count; } })()(); console.log(typeof a);//Output number console.log(a);//Output 1 console.log(a);//Output 1 console.log(a);//Output 1
Or write it in the following style:
var a=( function(){ var count=0; return function (){ count=count+1; return count; } }); console.log(typeof a);//Output function console.log(a()());//Output 1 console.log(a()());//Output 1 console.log(a()());//Output 1
Real time functions are often used together with closures, such as variable declaration and reference, modular development and so on.
The following is a simple example of counting function.
;(function (){ var count=0; function add(){//Increase count count=count+1; return count; } function reduce(){//Reduce count count=count-1; return count; } window.JS=function(){ return { add:add,reduce:reduce } } } )(); console.log(window.JS().add());//Output 1 console.log(window.JS().add());//Output 2 console.log(window.JS().add());//Output 3 console.log(window.JS().reduce());//Output 2 console.log(window.JS().reduce());//Output 1