Home >Web Front-end >JS Tutorial >Function type in ECMAScript_javascript skills

Function type in ECMAScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:57:071288browse

Speaking of the above in ECMAScript, I think the most interesting thing is the function. The reason why it is interesting is that the function is actually an object. Each function is an instance of the Function type and has the same properties and methods as other reference types. Since functions are objects, the function name is actually a pointer to the function object and will not be bound to a function. Functions are usually defined using function declaration syntax, as in the following example:

Copy code The code is as follows:

function sum(num1,num2)
{
Return num1 num2;
}

This is almost the same as the way below to define a function using a function expression.

Copy code The code is as follows:

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

The above code determines the variable sum and initializes it as a function. You will notice that there is no function name after the function keyword. This is because when defining a function using a function expression, there is no need to use the function name (the function can be referenced through the variable sum). Also, note that there is a semicolon at the end of the function, just like when declaring other variables.

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

Copy code The code is as follows:

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

From a technical point of view, this is a function expression. However, we do not recommend using this method to define functions because this syntax causes the code to be parsed twice (first to parse the regular ECMAScript code, and second to parse the string passed into the constructor), which affects performance. . However, this syntax is very intuitive for understanding the concept of "functions are objects and function names are pointers".

Since function names are simply pointers to functions, function names are no different from other variables containing object pointers. In other words, a function may have multiple names, as in the following example:

Copy code 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 find the sum of two values. Then, the variable anotherSum is declared and set to sum() equality (assigning the value of sum to anotherSum). Note that using a function name without parentheses accesses the function pointer, not calling the function. At this point, anotherSum and sum point to the same function, so anotherSum() can also be called and return the result. Even if sum is set to null, "disconnecting" it from the function, it still proves normal to call anotherSum().

The above is the entire content of this article. I hope it will be helpful to everyone learning javascript.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn