Several ways to call JavaScript functions

Source: Internet
Author: User
Tags anonymous

Functions are familiar to us programmers, and we can write functions almost every day, using functions. But, in JavaScript, you know a few statements about function calls? In the work, the commonly used function calls statements are one or two. So why do people know I still write blog here, nothing to kill everyone's time. If you want to know, please be patient, you will understand.

The function in JavaScript itself is a variable/value, so the function call is actually an expression, as shown in Figure 1

So the following code is the function call statement, which is also an expression statement:

FunctionName ();

In JavaScript, named functions can be invoked directly by using the above method, and anonymous functions can be invoked by referencing variables, and if no anonymous function is invoked? The following example illustrates these three scenarios:

Instance 1: The named function directly invokes function FnName () {//function body}

FnName ()//instance 2: The anonymous function invokes the var fnname = function () {//function body} by reference variable;

FnName ()//instance 3: Call with no referenced anonymous function (1) (function () {//Function Body} ());//Instance 4: Call with no referenced anonymous function (2) (function () {//Function Body}) ()//instance 5: Anonymous function without reference Call (3) void function () {//Function body} ();

The usage of instance 1,2 is more common, instance 4 is used more in many frameworks now, the example 3,5 see is relatively few, but each has its use.

Instance 3,4 are all used to "call a function and return a value", both of which have the parentheses, but the meanings are different. As illustrated in Figure 2, example 3:

Description of Instance 4:

In fact, example 3, 4 is basically consistent. But their operations are still different: instance 3 uses the force operator to make a function call operation, and instance 4 uses the force operator to declare the function direct, and returns a reference to the function itself, and then the function call operator "()" to manipulate the reference to the function.

PS: "Function call operator ()" Acts on the anonymous function itself in instance 3, whereas in instance 4 it is the result value that acts on an operation.

The last instance, 5, is used to invoke the function and ignore the return value. operator void is used to perform an operation on a subsequent function expression. The problem with this is that if you do not use the two operators of Void and (), can you make a function expression execute without using the following code directly?

Instance 6: Directly using the call function operator "()" function () {//Function Body} ()//Instance 7: Using the statement terminator ";" To execute the statement function () {//Function body} ();

Instance 6,7 appears to be right, but in fact none of them can be executed because they cannot be detected through the scripting engine's syntax. During the syntax detection phase, the script engine considers the following code:

function () {//Body}//or Functions FnName () {//function body}

The result is a function declaration, so using a named function in the instance 6,7 is not a syntax detection, because here is the function declaration, where the code for all instances 6,7 is located behind the function "()" has no grammatical meaning, and their code is parsed into

Example 6: Syntax interpretation function () {//function body};

()//instance 7: Ibid.

Since "function () {}" is interpreted as a complete grammatical structure (a function declaration statement), it is also equivalent to the existence of a statement terminator. therefore "();" is interpreted as a statement expression, and this is the wrong syntax. So, we can see grammatical errors.

So, this syntax error is for "();", not declared for the previous function, and the following code is slightly modified:

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

This is explained by the grammar, because the sentence is interpreted as a grammar.

Instance 6: Directly using the call function operator "()" function () {//function body};

(1,2);

Figure 4, is interpreted as two single value expressions, or it can be a single single value expression. However, it is important that this code is interpreted as a function of a direct volume declaration and an expression statement, so it does not act as "execute function and pass in parameters". If you really want to execute the function at the time of the statement, you can refer to the instance 3,4,5, using the "()" or the void operator to turn the function declaration into a "single value expression"

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

When the engine interprets such code, the following anonymous function is recognized as the operand, because operator Void is recognized first.

The above in JavaScript function call statement, said the last one I also see the book, but, I still did not understand, if the master can be a guide to the story that is too grateful. I also hope that this will help other students who just learn 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.