Several Methods for calling javascript Functions

Source: Internet
Author: User

Functions are not familiar to our programmers. We can write and use functions almost every day. But in javascript, how many function call statements do you know? At work, there are only one or two commonly used function call statements. That's why everyone knows that I am still writing a blog here, so it's okay to spend your time. If you want to know, please be patient and you will understand.

The function in javascript is a variable/value, so the function call is actually an expression, 1

Therefore, the following code is a function call Statement, which is also an expression statement:

FunctionName ();

In javascript, the name function can be called directly using the above method. An anonymous function can be called by referencing a variable. How can I call an anonymous function without reference? The following examples illustrate the three situations:

// Instance 1: The named function directly calls function fnName () {// function body}

FnName (); // instance 2: An anonymous function calls var fnName = function () {// function body} By referencing a variable };

FnName (); // instance 3: Call of an anonymous function that is not referenced (1) (function () {// function body} (); // instance 4: call of an anonymous function that has not been referenced (2) (function () {// function body}) (); // instance 5: Call of an anonymous function that has not been referenced (3) void function () {// function body }();

The usage of instance 1 and 2 is common. instance 4 is also used in many frameworks nowadays, and instance 3 and 5 are rarely seen, but each has its own use.

All instance 3 and 4 are used to "call a function and return value". The two expressions have the parentheses, but their meanings are different. 2. Description of instance 3:

Example 4:

In fact, instances 3 and 4 are basically the same. However, their operation procedures are still different: instance 3 uses the forced operator to execute the function call operation, and instance 4 uses the forced operator "function Direct Volume Declaration" expression, return a reference to the function itself, and then operate on the reference of the function through the function call operator.

Ps: the "function call operator ()" acts on the anonymous function itself in instance 3, while instance 4 acts on the result value of an operation.

The last instance 5 is used to "call a function and ignore the return value ". The void operator is used to enable the subsequent function expression to perform operations. However, the question is: Can I execute a function expression without using the void and () operators and directly using the following code?

// Instance 6: Call the function operator "()" function () {// function body} () // instance 7: Use the statement Terminator "; "To execute the statement function () {// function body }();

Instance 6 and 7 seem to be right, but in fact they cannot be executed because they cannot be detected by the script engine syntax. In the syntax detection stage, the script engine considers the following code:

Function () {// function body} // or function fnName () {// function body}

The result is a function declaration. Therefore, the use of the named function in instance 6 and instance 7 does not pass the syntax check. Because the function declaration is used here, the code of all instance 6 and instance 7 is located behind the function "() "No syntax, and their code is parsed

// Example 6: syntax explanation function () {// function body };

(); // Instance 7: Same as above

Since "function () {}" is interpreted as a complete syntax structure (function declaration statement), it is equivalent to an existing statement Terminator. Therefore, "();" is interpreted as a statement expression, which is a wrong syntax. Therefore, we can see a syntax error.

In this case, the syntax error is for "();", not for the previous function declaration. The following code is slightly modified:

// Example 6: Explain function () {// function body} (1, 2) by syntax)

In this way, the statement is interpreted as a syntax.

// Instance 6: Call the function operator "()" function () {// function body} directly };

(1, 2 );

Figure 4 is interpreted as two single-value expressions, or a single-value expression. But it is important that this Code is interpreted as a function to directly declare a number and an expression statement, so it cannot "execute a function and input parameters. If you really want to execute this function during declaration, you can refer to instance 3, 4, 5, and use the "()" or void operator to convert the function declaration to "single-value expression"

Void function () {// function body} (1, 2 );

When the engine interprets such code, the following anonymous functions are recognized as operands because the void operator is first recognized.

The above is the function call statement in javascript. To be honest, I also saw it in books, but I still haven't been very clear about it, if that expert can give me some advice on the original committee, I would be very grateful. I also hope this will help other students who are just learning javascript.

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.