Higher order functions: higher order looks like an esoteric term for advanced programming techniques, which I think I did when I first saw it.
Higher-order functions of JavaScript
However, a higher-order function is simply a function that takes a function as a parameter or return value . Take the following hello,world as a simple example.
var Moqi = function (p1) { this. Add = function (p2) { return' + p2; }; return add;}; // We can use this function Console.log (Moqi ('Hello') (' World '));
Maybe the process is a bit confusing, just look at the details.
typeof Moqi ('hello')//"function"Moqi ('Hello ' )//function (p2) {// return p1 + "+ p2; // }
In other words, Moqi (' Hello ') is a function, Moqi (' Hello ')
var m = Moqi ('Hello') m ('World') //"Hello,world"
From the above, higher-order functions can make the code more concise and efficient. Naturally, we can also create a function like this:
varMoqi =function (p1) {returnfunction (p2) {returnfunction (p3) {returnP1 +','+ P2 +' '+P3; } };}; Moqi ('Hello')(' World')('Phodal')//"Hello,world phodal"
Restore higher-order functions
Increasingly complex, the need to introduce higher-order function abstractions is the occurrence of repetitive or similar code. We then revert to the previous function:
var Moqi = function (p1) { this. Add = function (p2) { return function ( P3) { return', ' ' +p3; } }; return This . Add;};
And then create a new function.
var moqi = Function (p1) { this . Add = function (p2) { this . Add1 = function (p3) {
return
p1 + " , " + P2 + " ' +
Using the call method in JavaScript, you will have:
varMoqi =function (p1) {varSelf = This; function fd (p2) { This. Add1 =function (p3) {returnP1 +','+ P2 +' '+P3; }; } Self.add=function (p2) {Fd.call ( This, p2); return This. Add1; }; returnSelf.add;};
Examples of higher order functions
Add = function (b) { return a + b;}; function Math (func,array) { return func (array[0],array[1]);} Console.log (Math (add,[1,2])); // Math (add,[1,2]) // 3
Introduction to higher-order functions in JavaScript