Home > Web Front-end > JS Tutorial > body text

How to call js function_basic knowledge

WBOY
Release: 2016-05-16 16:49:44
Original
1179 people have browsed it

The ways to call Js functions are as follows:

(1) The named function directly calls

Copy code The code is as follows:

function foo ()
{
}
foo();

(2) The anonymous function is called by reference

Copy code The code is as follows:

fooRef = function()
{
}
fooRef();

(3) Anonymous function call without reference 1

Copy code The code is as follows:

(function()
{
}());

(4) Anonymous function call without reference 2

Copy code The code is as follows:

(function()
{
})();

(5) Anonymous function call without reference 3

Copy code The code is as follows:

void function()
{

}();


How to call js function_basic knowledge

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...

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template