The ways to call Js functions are as follows:
(1) The named function directly calls
(2) The anonymous function is called by reference
(3) Anonymous function call without reference 1
(4) Anonymous function call without reference 2
(5) Anonymous function call without reference 3
Figure 1.1 and Figure 1.2 show that the operation processes of these two expressions are different. In Figure 1.1, the coercion operator is used to perform the function call operation, while in Figure 1.2, the coercion operator is used to operate the "function directly" Quantity declares "this expression and returns a reference to the function itself, and then operates the function reference through the function call operation "()". The last anonymous function call void function(){}(); above is used to call the function and ignore the return value. The operator void is used to make the subsequent function expression perform operations. If we don't use "void" and the coercion operation "()", can the code be executed:
(1)function(){}() //Use ''()" to force the call
(2)function(){}(); //Use ";" to execute statements
The script engine will think that function(){} is a function declaration, thus failing the syntax detection, and the code will be parsed like this:
function(){};();
function(){} is interpreted as a statement, while "();" is interpreted as a separate line, so a syntax error will be reported. Why do you know that the error is caused by "();"? We change it to the following code:
function(){}(1);
This will be interpreted by the engine as:
fucntion(){};
(1); //Single value expression
Thus passed the grammar check...