SoFunction
Updated on 2025-03-09

Function function type in ECMAScript

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

Copy the codeThe 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 function expressions.

Copy the codeThe code is as follows:

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

The above code defines the variable sum and initializes it into a function. You will notice that there is no function name after the function keyword. This is because when defining a function using function expressions, it is not necessary to use the function name (the function can be referenced by 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 will always be regarded as the body of the function, while the previous parameter enumerates the parameters of the new function. As shown in the following example:

Copy the codeThe 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 (the first time parsing regular ECMAScript code, and the second time parsing strings passed in the constructor), thus affecting performance. However, this syntax is very intuitive to understand the concept of "a function is an object and a function name is a pointer".

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

Copy the codeThe 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() to find the sum of two values. Then, there is a variable anotherSum declared and set it to sum() equal (assign the value of sum to anotherSum). Note that using function names without parentheses is to access function pointers, not to call functions. At this time, anotherSum and sum point to the same function, so anotherSum() can also be called and the result is returned. Even if sum is set to null, letting it "break away" from the function, it is still proven that anotherSum() is called normally.

The above is all about this article. I hope it will be helpful to everyone to learn JavaScript.