Home  >  Article  >  Web Front-end  >  A brief analysis of the understanding of functions in JS

A brief analysis of the understanding of functions in JS

高洛峰
高洛峰Original
2017-01-21 09:29:231136browse

Text: We know that in js, a 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. Therefore, the function name is actually a pointer to the function object and is not tied to a function. In addition to the two common definition methods (see below), there is another definition method that can more intuitively reflect this concept:

var sum = new Function("num1", "num2", "return num1 + num2"); //不推荐

The constructor of Function can receive any number parameters, but the last parameter is always considered the function body. One disadvantage of this method is that it will cause the code to be parsed twice. The first time is to parse the basic ES code, and the second time is to parse the string passed into the constructor. This will lead to a decrease in performance. Write here It helps to understand the concept that functions in js are actually objects.

(1) There is no concept of overloading in js functions

When it comes to the concept of overloading, let’s make an analogy to how overloading in Java is implemented: In Java, methods are through methods A signature uniquely identifies a method. The method signature includes: method name, number of parameters, parameter order and parameter type. Therefore, if the method names of two methods are the same but other signature elements are different, the compiler will consider them to be two different methods, so that different methods with the same name can exist to realize the concept of overloading. (Quote: How to deeply understand why there is no overloading in js - manxisuo's answer in segmentfault).

As we said above, the function name in js is actually a pointer to the function object. Therefore, the function name can be said to be the unique identifier of a function. It has nothing to do with the parameter list, so it will not appear. Two functions with the same name (because a pointer can only point to one object at the same time) do not have the concept of overloading. For example:

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

The above can be written in another way, which will be more intuitive:

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

This way it is obvious to see The above line points sum to function(num1,num2), and then points sum to function(num1, num2, num3), causing the second pointer to overwrite the first pointer, so it is more obvious to see There is no concept of overloading in js.

(2) The difference between function declaration and function expression

We know that there are two commonly used methods of defining functions: function declaration and function expression.

Function declaration can be said to be the most common definition method, as shown in the following example:

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

Function expression, this definition method is used in closures and some frameworks There are many, such as the common $scope.doSomething = function(){...} in angular:

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

The above two definition methods are almost the same, but there is a small difference, please see the following example :

//函数声明
alert(sum(100, 100));
function sum(num1, num2){
return num1 + num2;
}
//函数表达式
alert(sum(100, 100));
var sum = function(num1, num2){
return num1 + num2;
};

The two pieces of code don’t seem to be much different, just the way the function is defined is different. But the fact is that the first paragraph works fine but the second paragraph throws an error. This is because, before the code starts executing, the parser has already read and added function declarations to the execution environment through a process called function declaration hoisting. Simply put, the parser first places the function declaration at the top of the source tree. In the code below, the function is located in an initialization statement (it is not raised to the top). Simply put, before executing the assignment statement, sum does not point to any function, so calling the function before will Report an error.

The above is the understanding of functions in JS (Basics) introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. of. I would also like to thank you all for your support of the PHP Chinese website!

For more articles on the understanding of functions in JS, please pay attention to the PHP Chinese website!

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