There are several ways to call Js function:
(1) Call the named function directly
The code copy is as follows:
function foo()
{
}
foo();
(2) Anonymous functions are called by reference
The code copy is as follows:
fooRef = function()
{
}
fooRef();
(3) Anonymous function call without reference 1
The code copy is as follows:
(function()
{
}());
(4) Anonymous function call without references 2
The code copy is as follows:
(function()
{
})();
(5) Anonymous function call without references 3
The code copy is as follows:
void function()
{
}();
Figure 1.1 and Figure 1.2 show that the operation process of these two expressions is different. In Figure 1.1, the forced operator is used to enable the function call operation to be executed. In Figure 1.2, the forced operator is used to operate the expression "function direct quantity declaration" and returns a reference to the function itself, and then the function reference is operated through the function call operation "()". The last anonymous function above calls void function(){}(); is used to call the function and ignore the return value. The operator void is used to make the function expressions that follow it perform operations. If we do not use "void" and forced operation "()", can the code be executed:
(1) function(){}() // Use ''()" to force call
(2) function(){}(); //Use ";" to execute the statement
The script engine will think that function(){} is a function declaration, so that it cannot pass syntax detection, and the code is parsed like this:
function(){};();
function(){} is interpreted as a declaration, while "();" is interpreted independently as a line, thus a syntax error will be reported. Why do you know that it is an error caused by "();"? We change it to the following code:
function(){}(1);
This will be explained by the engine as:
fucntion(){};
(1); //Single-value expression
Therefore, the syntax detection passed...