Analysis of functions in JS

Source: Internet
Author: User
We know that in js, a Function is actually an object. Every Function is a Function-type instance and has attributes and methods like other reference types. Next, let's talk about the understanding of function functions in JS. Let's take a look at the text: we know that in js, a Function is actually an object, and every function is a Function-type instance, it also has attributes and methods like other reference types. Therefore, the function name is actually a pointer to a function object and is not bound to a function. In addition to the two common definition methods (see the following), there is also a way to more intuitively reflect this concept:

Var sum = new Function ("num1", "num2", "return num1 + num2"); // not recommended

Function constructor can receive any number of parameters, but the last parameter is always regarded as the Function body. This method has the disadvantage of parsing the code twice. The first is the basic ES code parsing, and the second is to parse the strings passed into the constructor, which will lead to performance degradation, writing here helps to understand the concept that functions in js are actually objects.

(1) js functions are not overloaded.

Speaking of the concept of overloading, we can compare how the overloading in Java is implemented: in Java, the method is uniquely identified by the method signature. The method signature includes the method name, parameter quantity, Parameter order, and parameter type. Therefore, the two methods have the same name, while the other signature elements are different. The Compiler considers them as two different methods, so that different methods with the same name can exist to implement the concept of overloading. (Reference: How can I thoroughly understand why manxisuo's answer in segmentfault is not overloaded in js ).

As we have said above, the function name in js is actually a pointer to the function object, so the function name can be said to be the unique identifier of a function and has nothing to do with the parameter list, therefore, there will be no two functions with the same name (because a pointer can only point to one object at the same time) and there is no concept of overloading. 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 is more intuitive:

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

In this way, we can see that the above line points sum to function (num1, num2), and then points sum to function (num1, num2, num3 ), as a result, the second point overwrites the first point. Therefore, we can see that js does not have the concept of overloading.

(2) differences between function declaration and function expression

We know that there are two commonly used function definition methods: function declaration and function expression.

Function declaration is the most common definition method, as shown in the following example:

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

Function expressions, which are used in closures and some frameworks. For example, $ scope. doSomething = function () {...} in angular (){...}:

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

The two methods are almost the same, but there is a small difference. See the following example:

// Function declaration alert (sum (100,100); function sum (num1, num2) {return num1 + num2;} // function expression alert (sum (100,100 )); var sum = function (num1, num2) {return num1 + num2 ;};

The two codes seem to be slightly different, but the function definition method is different. But the fact is that the first paragraph can run normally, and the second paragraph will report an error. This is because, before the Code starts to be executed, the parser reads and adds the function declaration to the execution environment through a process called function declaration escalation. In short, the parser first places the function declaration on the top of the source code tree. In the following code, the function is located in an initialization Statement (it will not be promoted to the top). In short, sum does not point to any function until the value assignment statement is executed, therefore, if you call a function before, an error is returned.

The above section describes how to understand function functions in JavaScript (basic). I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. I would like to thank you for your support for PHP chinnet!

For more information about how to understand function functions in Javascript, see PHP!

Related Article

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.