A brief analysis of JavaScript function type

Source: Internet
Author: User

The functions in JavaScript are actually objects, each of which is an instance of a function type, and all have properties and methods like other reference types. Because the function is an object, the function name is actually just a pointer to the function object, saving the address value of the function in the heap memory.

1, define the function of the three ways:

1.1. Function declaration method

1 function sum (NUM1, num2) {2return num1 + num2; 3 }   

1.2. Function expression

1 var function (NUM1, num2) {2     return NUM1 + num2; 3 }

1.3. Use the function constructor to pass in the named parameter and function body in turn. This approach is not recommended, but it is easy to understand that the function name is a pointer and the function is an object.

1 var New Function ("Sum1", "sum2", "return sum1 + sum2");  // Not recommended

2, function is object, function name is pointer

The following two lines of code if you do not understand the difference, it means that you are the function is an object, the function name is a pointer does not understand. The first line of code is to assign the address value of the function object pointed to by sum to Anothersum, which is only an assignment operation, and the SUM function is not executed. In this case, the Anothersum and sum objects are stored in the stack memory, while the function objects are stored in the heap memory, and the anothersum and sum are stored as the address values of the function objects in the heap memory. The second line of code here is to execute the SUM function and assign the return value to another. The second function is defined in such a way that it is better to differentiate them.

1 var anothersum = sum; 2 var anothersum = SUM ();

3. Why are there no overloads?

Converting the above two methods into the following two methods is a good way to understand why there is no overloading, because the function name is just a pointer to the function object, and when the second function with the same name is defined, add points to the new function object.

1 functionAdd (num) {2     returnNum + 100;3 }4 functionAdd (num) {5     returnNum + 200;6 }7 8 varAdd =function(num) {9     returnNum + 100;Ten } One  AAdd =function(num) { -     returnNum + 200; - }; the  -Alert (Add (50));// -

4. The difference between function declaration and function expression

The difference between the two ways of defining a function is that when the parser loads the data into the execution environment, the parser first reads the function declaration and makes it valid before executing any code, and the function expression executes only when the parser executes the code line where it is located. In addition to this distinction, the two ways of defining a function are equivalent. Look at the following example

1Alert (SUM (10, 20));//Output2 functionsum (NUM1, num2) {3 returnNUM1 +num2;4 }5 6Alert (SUM (10, 20));//error, sum is not a function, it is clear that the function expression has not been read to the parser7 varsum =function(NUM1, num2) {8 returnNUM1 +num2;9}

5, as a function of values, parameters, and in-depth understanding of the sort method of the comparator function rules, according to the sequence of elements of the array to compare, if the return is positive, it proves that Object1 is larger than object2, negative opposite, 0 equal, Same as the Compare method of the comparator interface in Java.

1 functionCompare (PropertyName) {2     return function(Object1, Object2) {//function is returned as a value3Value1 =Object1[propertyname];4value2 =Object2[propertyname];5Console.log (value1 + "" +value2);6         if(Value1 >value2) {7             return1;8}Else if(Value1 <value2) {9             return-1;Ten}Else{ One             return0; A         } -     } - } the  - vardata = [{name: "Zhangsan", Age:23},{name: "Lisi", age:25}]; - varNewData = Data.sort (Compare ("name"));//take a function as a parameter -Console.log (Newdata[0].name); Lisi

The Compare function is passed as a parameter to the array's sort function, and the function is returned as a value within the Compare function, and the compare function can be sorted according to the object attributes you passed in.

Because the function is also an object, it also has properties and methods, about the function of the properties and methods are not introduced here, there is a more important attribute prototype, later learning.

  

A brief analysis of JavaScript function type

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.