Introduction to Javascript high-order functions, javascript High-Order Functions
Higher-order function-if a function receives a parameter or returns a value of the function, we can call this function a high-order function. As we all know, JavaScript is a weak type of language: JavaScript Functions neither define input parameters nor output values of functions, but can become parameters, it can also be an output value, which reflects the native support of JavaScript for higher-order functions.
1. High-order functions with parameters:
Function funcTest (f) {// you can easily determine whether the real parameter is a function if (typeof f) = "function") {f () ;}} funcTest (function () {});
This is a simple high-level function that uses parameters as functions. When you call funcTest, enter a function as the parameter and execute the anonymous function in funcTest. Of course, such code snippets have no practical significance.
1. return values:
function funcTest(){return function(){};}var f=funcTest();
Call funcTest to return a function.
2. A complex example:
// Number type addition function addInt (a, B) {return parseInt (a) + parseInt (B);} // String type addition function addString (a, B) {return. toString () + B. toString () ;}function add (type) {if (type = "string") {return addString ;}else {return addInt ;}} var data1 = add ("string") ("1", "2"); // 12var data2 = add ("int") ("1", "2 "); // 3
The preceding example separates the addition of the String type from the addition of the Number type. Call the add function. If the input parameter is "string", a string concatenation function is output. If the input parameter is "int", a number addition function is output.
Iii. practical functions of higher-order functions:
The code example above shows what a high-order function is. Let's take a look at the relationship between the high-order function and our actual programming:
1. Callback Function
Function callback (value) {alert (value);} function funcTest (value, f) // f real parameter detection, check if f is a function if (typeof callback = 'function') {f (value) ;}} funcTest ('1', callback); // 1
For example, when you call funcTest, funcTest calls the callback function internally to implement callback.
2. Data Filtering and sorting algorithms
Var arr = [,]; // Sorting Algorithm function funcComp (a, B) {if (a <B) {return-1 ;} else if (a> B) {return 1 ;}else {return 0 ;}// filter algorithm function funcFilter (item, index, array) {return item >=5 ;} // arrange arr in an array order. sort (funcComp); alert (arr. join (','); //, // Filter Array var arrFilter = arr. filter (funcFilter); alert (arr. join (',') // 5, 7, 9, 11
3. DOM element event Definition
<Html> <title> </title> <body> <input type = "button" value = "ClickMe" id = "myBtn"> <script type = "text/javascript> var btnClick = document. getElementById ("myBtn"); // The test environment is FireFoxbtnClick. addEventListener ("click", function (e) {alert ("I'm a button! "); // I'm a button}, false); </script> </body>
In the above example, a button with the id of myBtn is defined in the document, and the js script adds a click event for it. The second parameter of addEventListener is a function.
Conclusion: High-order functions are not a patent of JavaScript, but they are definitely a powerful tool for JavaScript programming. Higher-order functions are actually the re-Abstraction of basic algorithms. We can use this to improve code abstraction and maximize code reuse, compile code that is more concise and easier to refactor.