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:
This is almost the same as the way below to define a function using a function expression.
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:
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:
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.