function type-functions as values

Source: Internet
Author: User
Tags array sort

function as a value

Because the function name in ECMAScript itself is a variable, the function can also be used as a value. That is, not only can a function be passed to another function like a parameter, but a function can be returned as the result of another function. Take a look at the following function.

function Callsomefunction (somefunction,someargumnt) {  return  someFunction (someargument) ;  }

This function accepts two parameters. The first parameter should be a function, and the second parameter should be a value to pass to the function. Then, you can pass the function as in the following example.

function add10 (num) {returnnum+Ten ; }varResult=callsomefunction (ADD10,Ten); alert (result);// -function getgreeting (name) {return "Hello,"+name; }varResult2=callsomefunction (GetGreeting,"Nicholas"); alert (RESULT2);//Hello,nicholas

The Callsomefunction () function here is generic, that is, whatever function is passed in the first argument, it returns the result after the first argument is executed. Therefore, the above example is passed to Callsomefunction () ADD10 and getgreeting instead of the result after they are executed.

Of course, you can return another function from one function, and this is a very useful technique. For example, suppose you have an array of objects, and we want to sort the arrays based on an object property. The comparison function that is passed to the array sort () method will receive two parameters, that is, the value to compare. However, we need a way to indicate which property to sort by. To solve this problem, you can define a function that receives a property name and then creates a comparison function based on the property name, which is the definition of the function.

function Createcomparisonfunction (PropertyName) {returnfunction (object1,object2) {varvalue1=Object1[propertyname]; varValue2=Object2[propertyname]; if(value1<value2) {      return-1; } Else if(value1>value2) {      return 1; } Else{      return 0;  }  }; }

This function definition looks a bit complicated, but in fact it is simply nesting another function in one function, and the inner function is preceded by a return operator. After the intrinsic function receives the propertyname parameter, it uses square brackets to denote the value of the given property. Once you have obtained the desired attribute value, it is very simple to define the comparison function. The above function can be used as in the example below.

    varData=[{name:"Zachary", Age: -},{name:"Nicholas", Age: in}]; Data.sort (Createcomparisonfunction ("name")); Alert (data[0],name);//Nicholas;Data.sort (Createcomparisonfunction (" Age")); Alert (data[0],name);//Zachary

Here, we create an array of data with two objects, each of which contains a Name property and an age property. By default, the sort () method invokes the ToString () method of each object to determine their order, but the resulting results often do not conform to the human mind habit, so we call Createcomparisonfunction ("name") Method creates a comparison function to sort by the Name property of each object. The first of the results is the name "Nicholas", and age is the object of 29. We then use the comparison function returned by creatcomparisonfunction ("Age"), which is sorted by the object's age property. The result is that the name value is "Zachary", and that the age value is 28 for the first bit of the object.

function type-functions as values

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.