_javascript techniques for function types in ECMAScript

Source: Internet
Author: User

The most interesting thing about ECMAScript, I think, is the function, the interesting source, is that the function is actually an object. Each function is an instance of a function type and has properties and methods as well as other reference types. Because a function is an object, the function name is actually a pointer to a function object and is not bound to a function. Functions are usually defined by using the function declaration syntax, as in the following example:

Copy Code code as follows:

function sum (num1,num2)
{
return num1+num2;
}

This is almost the same as using function expressions to define functions in the following ways.

Copy Code code as follows:

var sum=function (num1,num2)
{
return num1+num2;
};

The above code set the variable sum and initializes it to a function. You will notice that there are no function names after the functions keyword. This is because when you use a function expression to define a function, it is not necessary to use the function name (you can refer to the function by the sum of the variables). Also, note that there is a semicolon at the end of the function, just as when declaring other variables.

The last way to define a function is to use the function constructor. A function constructor can accept any number of arguments, but the last argument is always treated as a body of functions, and the preceding argument enumerates the parameters of the new function. The following example:

Copy Code code as follows:

var sum=new Function ("Num1", "num2", "return num1+num2");/Not recommended

From a technical standpoint, this is a function expression. However, we do not recommend using this method to define a function, because this syntax can result in the parsing of two of times code (the first time the normal ECMAScript code is parsed, the second is parsing the string in the incoming constructor), which affects performance. However, this syntax is very intuitive to understand the notion that functions are objects and function names are pointers.

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

Copy Code code 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 called sum (), which is used to find two values. Then, there is a declaration of the variable anothersum and sets it to sum () equal (assigns the value of sum to Anothersum). Note that using a function name without parentheses is to access the function pointer, not to call the function. At this point, anothersum and sum point to the same function, so anothersum () can also be invoked and returned the result. Even if sum is set to NULL, it is "severed" from the function, but it can still be proved that normal call Anothersum ().

The above mentioned is the entire content of this article, hope to be able to learn JavaScript to be helpful to everybody.

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.