Javascript High-order function introduction _javascript Tips

Source: Internet
Author: User

Higher-order function-If a function receives a value that is or returns a function, then we can call this function a High-order function. higher-order As we all know, JavaScript is a weak type of language: JavaScript functions are neither input parameters nor function output values strong definition and type checking, then the function can become a parameter, can also be output values, This embodies JavaScript's native support for higher-order functions.

One, the function of the higher order function of the parameter:

function Functest (f) {  
//easy to determine whether the arguments are functions
if ((typeof f) = = "function") {
f ();
}}
Functest (function () {});

This is a simple high-order function that takes a parameter as a function. When calling Functest, enter a function as an argument that executes the anonymous function of this input inside the functest, which, of course, has little practical significance.

One, the return value is a function of higher order function:

function Functest () {return
function () {};
}
var f=functest ();

Call Functest returns a function.

Second, a more complex example:

Number type add  
function AddInt (a,b) {return
parseint (a) +parseint (b);
 }
String type add
function addstring (a,b) {return
a.tostring () + b.tostring (); 
}  
function Add (type) {
if (type=== "string") {return
addstring
} else{return
addint 
}
}
var data1=add ("string") ("1", "2");
var data2=add ("int") ("1", "2");
3

The above example implements a separation of string type addition and number type addition. Call the Add function output a string concatenation function if the input parameter is "string", or output the numeric addition function if the input parameter is "int".

Third, the practical role of higher-order functions:

The above code example basically shows what a high-order function is, and here's how the higher-order function relates to our actual programming:

1, callback function

function callback (value) {
alert (value);
}
function Functest (value,f)
//f argument detection, check if f is functions 
if (typeof callback=== ' function ') {
f (value);}} Functest (' 1 ', callback);
1

Example when calling Functest, the callback function is called inside the functest, that is, the callback is implemented.

2, data filtering and sorting algorithm

var arr=[0,2,11,9,7,5];
Sort algorithm
function Funccomp (a,b) {
if (a<b) {
return-1;
} else if (a>b) {return
1;
} else{return
0;
}
Filtering algorithm
function Funcfilter (item,index,array) {return
item>=5;
}
Array Order
Arr.sort (funccomp);
Alert (Arr.join (', '));
0,2,5,7,9,11
//filter array
var arrfilter=arr.filter (funcfilter);
Alert (Arr.join (', '))
//5,7,9,11

3,dom element Event Definition

 
 

In the example above, a button with ID mybtn is defined in the document, and the JS script adds a Click event to it, where AddEventListener's second argument is a function.

Conclusion: higher-order functions are not a patent for JavaScript, but are definitely a tool for JavaScript programming. Higher-order functions are actually a new abstraction of the basic algorithm, which we can use to improve the abstraction of the code, achieve maximum code reuse, and write simpler and more refactoring code.

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.