Js function call Method

Source: Internet
Author: User

Js function calls can be performed in the following ways:

(1) Direct call of the named Function
Copy codeThe Code is as follows:
Function foo ()
{
}
Foo ();

(2) Anonymous functions are called through reference.
Copy codeThe Code is as follows:
FooRef = function ()
{
}
FooRef ();

(3) call an anonymous function that is not referenced 1
Copy codeThe Code is as follows:
(Function ()
{
}());

(4) Anonymous function calls not referenced 2
Copy codeThe Code is as follows:
(Function ()
{
})();
 

(5) Anonymous function calls not referenced 3
 Copy codeThe Code is as follows:
Void function ()
{

}();


Figure 1.1 and Figure 1.2 show that the operation processes of these two expressions are different. In Figure 1.1, the mandatory operator is used to execute the function call operation, in Figure 1.2, a forced operator is used to calculate the expression "function Direct Volume Declaration", return a reference to the function itself, and then operate on the function reference through the function call operation. The last anonymous function called void function () {} (); is used to call the function and ignore the return value. The void operator is used to make the function expression following it perform operations. If we do not use "void" and force operation "()", can the code be executed:

(1) function () {} () // use ''()" to force call

(2) function () {}(); // use ";" to execute the statement

The script engine considers function () {} as a function declaration, which does not pass syntax detection. The code is parsed as follows:

Function (){};();

Function () {} is interpreted as a declaration, and "();" is interpreted as a separate line. Therefore, a syntax error is reported. Why do you know it is "(); "error? Let's change it to the following code:

Function () {}( 1 );

This will be interpreted by the engine:

Fucntion (){};

(1); // single value expression

Therefore, the syntax detection is passed...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.