This is an interesting thing, and it may also be a description of the power of JavaScript objects. What we're going to do is output a hello,world in the last article, and the input is print (' Hello ') (' World '), which is called higher-order function.
Higher order functions
High-order looks like an esoteric term for advanced programming techniques that I thought I saw at first.
Higher-order functions of JavaScript
However, higher-order functions are simply functions that take functions as arguments or return values. Take the above Hello,world as a simple example.
Copy Code code as follows:
var Moqi = function (p1) {
This.add = function (p2) {
return p1 + ' + p2;
};
return add;
};
We can then use this function
Copy Code code as follows:
Console.log (Moqi (' Hello ') (' World '));
Maybe the process is a little confusing, look at the details.
Copy Code code as follows:
> typeof Moqi (' Hello ')
<-"function"
> Moqi (' Hello ')
<-function (p2) {
return p1 + ' + p2;
}
That is to say actually Moqi (' Hello ') is a function, Moqi (' Hello ')
Copy Code code as follows:
> 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 to facilitate:
Copy Code code as follows:
> Moqi (' Hello ') (' World ') (' Phodal ')
> "Hello,world phodal"
And then there's this one function.
Copy Code code as follows:
var Moqi = function (p1) {
return function (p2) {
return function (p3) {
return p1 + ', ' + p2 + ' +p3;
}
};
};
Restore higher-order functions
Increasingly complex, the need to introduce higher-order function abstraction signal is duplicated or similar code. We then revert to the previous function in a step-by-step way:
Copy Code code as follows:
var Moqi = function (p1) {
This.add = function (p2) {
return function (p3) {
return p1 + ', ' + p2 + ' +p3;
}
};
return this.add;
};
And then create a new function.
Copy Code code 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, you will have:
Copy Code code 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;
};
Higher order function Instance
The examples above are just for fun, and the following examples are really used.
Copy Code code 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 in the example above is a parameter, and the return is just a function. As in jquery, a function is used to
Copy Code code 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 use of higher-order functions to master the importance of good JS.