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:
?
| 1 2 3 4 5 6 |
function Functest (f) {//simple to determine whether the argument is a functional 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:
?
| 1 2 3 4 |
function Functest () {return function () {};} var f=functest (); |
Call Functest returns a function.
Second, a more complex example:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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
?
| 1 2 3 4 5 6 7 8 |
function callback (value) {alert (value);} function Functest (value,f)//f argument detection, check F for function 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
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22-23 |
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.