Introduction to high-order functions in Javascript, javascript High-Order Functions
This is an interesting thing, and it may also show the strength of Javascript objects. What we need to do is output A Hello, World as we mentioned in the previous article, while the input is print ('hello') ('World '), this is the so-called high-order function.
High-order functions
The higher level seems like an esoteric term of an advanced programming technology. I thought so at the beginning.
Javascript high-level functions
However, higher-order functions only use functions as parameters or return values. The preceding Hello, World is a simple example.
Copy codeThe Code is as follows:
Var Moqi = function (p1 ){
This. add = function (p2 ){
Return p1 + ''+ p2;
};
Return add;
};
We can use this function in this way.
Copy codeThe Code is as follows:
Console. log (Moqi ('hello') ('World '));
Maybe this process is a bit messy. Let's look at it in detail.
Copy codeThe Code is as follows:
> Typeof Moqi ('hello ')
<-"Function"
> Moqi ('hello ')
<-Function (p2 ){
Return p1 + ''+ p2;
}
That is to say, Moqi ('hello') is a function, Moqi ('hello ')
Copy codeThe Code is as follows:
> Var m = Moqi ('hello ')
> M ('World ')
> "Hello, World"
From the above situation, higher-order functions can make the code more concise and efficient. Naturally, we can also create a function to facilitate:
Copy codeThe Code is as follows:
> Moqi ('hello') ('World') ('phodal ')
> "Hello, World Phodal"
So there is such a function.
Copy codeThe Code is as follows:
Var Moqi = function (p1 ){
Return function (p2 ){
Return function (p3 ){
Return p1 + ',' + p2 + ''+ p3;
}
};
};
Restore high-level functions
More and more complex, the signal that requires the introduction of high-level function abstraction is repeated or similar code. Then, we first step back to the previous function:
Copy codeThe Code 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.
Copy codeThe Code 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;
};
When you use the call method in javascript, the following occurs:
Copy codeThe Code 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;
};
High-Order Function instances
The above example is just for fun. The following example is a real application.
Copy codeThe Code 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
In the preceding example, add is a parameter, while return is a function. For example, there is a function in jQuery
Copy codeThe Code 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 application. It can be seen that higher-order functions are important for mastering JS ..