This is an interesting thing, which may also illustrate the power of Javascript objects. What we have to do is to output a Hello, World as mentioned in the previous article, and the input is print('Hello')('World'), and this is the so-called higher-order function.
Advanced functions
Advanced order looks like an esoteric term for advanced programming techniques, and I thought so when I first saw it.
Higher-order functions of Javascript
However, higher-order functions are just functions that take functions as arguments or return values. Hello and World are the above as a simple example.
The code copy is as follows:
var Moqi = function(p1){
this.add = function (p2){
return p1 + ' ' + p2;
};
return add;
};
We can use this function like this
The code copy is as follows:
console.log(Moqi('Hello')('World'));
Maybe this process is a bit confusing, let's see if it's more detailed.
The code copy is as follows:
> typeof Moqi('Hello')
<- "function"
> Moqi('Hello')
<- function (p2){
return p1 + ' ' + p2;
}
That is to say, in fact, Moqi('Hello') is a function, Moqi('Hello')
The code copy is as follows:
> var m = Moqi('Hello')
> m('World')
> "Hello,World"
Judging from the above situation, higher-order functions can make the code more concise and efficient. Naturally, we can also create a function to facilitate:
The code copy is as follows:
> Moqi('Hello')('World')('Phodal')
> "Hello,World Phodal"
So there is such a function
The code copy is as follows:
var Moqi = function(p1){
return function (p2){
return function(p3){
return p1 + ',' + p2 + ' ' +p3;
}
};
};
Restore higher-order functions
The signals that need to introduce higher-order functions abstraction are duplicate or similar codes. Then, we restore to the previous function step by step:
The code copy is as follows:
var Moqi = function(p1){
this.add = function (p2){
return function(p3){
return p1 + ',' + p2 + ' ' +p3;
}
};
return this.add;
};
Then create a new function
The code copy is as follows:
var Moqi = function(p1){
this.add = function (p2){
this.add1 = function(p3){
return p1 + ',' + p2 + ' ' +p3;
};
return this.add1;
};
return this.add;
};
Using the call method in javascript, there will be:
The code copy is as follows:
var Moqi = function(p1){
var self = this;
function fd(p2) {
this.add1 = function (p3) {
return p1 + ',' + p2 + ' ' + p3;
};
}
self.add = function (p2){
fd.call(this, p2);
return this.add1;
};
return self.add;
};
Advanced function examples
The above example is just for fun, and the following example is a real application.
The code copy is as follows:
add = function(a,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
The add passed in the example above is a parameter, and the return is just a function. For example, there is a function in jQuery
The code copy is as follows:
// Convert dashed to camelCase; used by the css and data modules
// Microsoft forgot to hump their vendor prefix (#9572)
camelCase: function( string ) {
return string.replace( rmsPrefix, "ms-" ).replace( rdashAlpha, fcamelCase );
},
This is also the case with the use of higher-order functions in order to master JS. .