Function Type in ECMAScript

Source: Internet
Author: User

Function Type in ECMAScript

This article mainly introduces information about function types in ECMAScript. For more information, see

The most interesting part of ECMAScript is that the function is actually an object. Each Function is a Function-type instance and has attributes and methods like other reference types. Because the function is an object, the function name is actually a pointer to the function object and will not be bound to a function. A function is usually defined using the function declaration syntax, as shown in the following example:

The Code is as follows:

Function sum (num1, num2)

{

Return num1 + num2;

}

This is almost the same as the following method of defining a function using a function expression.

The Code is as follows:

Var sum = function (num1, num2)

{

Return num1 + num2;

};

The above Code sets the variable sum and initializes it as a function. You will notice that the function name is not followed by the function keyword. This is because when using a function expression to define a function, there is no need to use the function name (the function can be referenced through the sum variable ). In addition, note that a semicolon is at the end of the function, just like when other variables are declared.

The last way to define a Function is to use the Function to construct a Function. The Function constructor can accept any number of parameters, but the last parameter is always regarded as the Function body, and the previous parameter lists the parameters of the new Function. Example:

Copy the Code as follows:

Var sum = new Function ("num1", "num2", "return num1 + num2"); // not recommended

Technically speaking, this is a function expression. However, we do not recommend using this method to define a function, because this syntax will cause parsing twice of the Code (the first time parsing the regular ECMAScript code, the second time parsing the strings passed into the constructor ), this affects performance. However, this syntax is very intuitive for understanding the concept of "functions are objects and function names are pointers.

Since the function name is only a pointer to the function, the function name is no different from other variables that contain the object pointer. In other words, a function may have multiple names, as shown in the following example:

The Code is as follows:

Function sum (num1, num2)

{

Return num1 + num2;

}

Alert (sum (10, 10); // 20

Var anotherSum = sum;

Alert (anotherSum (10, 10); // 20

Sum = null;

Alert (anotherSum (10, 10); // 20

The above code first defines a function named sum (), which is used to evaluate the sum of two values. The variable anotherSum is displayed and set to sum () to be equal (the sum value is assigned to anotherSum ). Note that function names without parentheses are used to access function pointers rather than call functions. In this case, anotherSum and sum point to the same function, so anotherSum () can also be called and return results. Even if sum is set to null and it is "disconnected" from the function, it still proves that anotherSum () is called normally ().

The above is all the content of this article, hoping to help you learn javascript.

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.