JavaScript functions are one of the applications of higher-order functions. What is a higher-order function? In fact, if you pass a function as an argument, it’s a higher-order function. Callbacks, for example, which you all encounter in your daily development, are higher-order functions. So the question is, what is a function Corrification? Function corrification is a technique for converting a function that takes multiple arguments into a series of functions that take a single argument. Here’s a simple chestnut:
function add(a,b,c){
return a+b+c
}
// After currization
function addCurrying(a){
return function(b){
return a+b
}
}
// Call the original function
add(1.2);
// The call method of currization
addCurrying(1) (2);
Copy the code
In effect, the function corrification changes the x and y arguments of add to a function that receives x and then returns a function that processes y arguments. Thinking: If the number of parameters is unknown, it is obvious that the above situation does not have universality and cannot meet the actual needs. For example, addCurrying(1,2)(2,3,4,5,6)(3)(4) can be used with any argument. Obviously, there are two characteristics of horizontal and vertical. Horizontal means that countless methods can be executed, and vertical means that the number of parameters is uncertain. So following the example above, let’s move on.
// We can pass any number of parameters
function addCurrying(){
let _args = Array.from(arguments);
return function(){ _args.push(... arguments);return _args.reduce((pre,cur) = > pre+cur,0)}}// Execute the result
addCurrying(1) (2); / / 3
addCurrying(1.2) (3.4.5); / / 15
Copy the code
The above method uses the knowledge point:
arguments
Represents a class array of arguments inside a function (note: none in arrow functions)arguments
)Array.from
The class array is converted to a real array, convenient for later cumulative calculation. There are many ways to convert, andArray.of(... arguments)
orArray.prototype.slice.call(arguments)
Etc.closure
, store all the parameters_args
In the arrayreduce
The sum is the result
The example above solved the vertical problem, but what about the horizontal problem? Make sure that the return result is always function.
function addCurrying(){
let _args = Array.from(arguments);
let adapter = function(){
// Store the parameters of each execution method in closure variables_args.push(... arguments);return adapter
}
return adapter
}
Copy the code
OK, this solves the problem of horizontal infinite loop calls. So the question is, how do you do that? The answer is to use the toString implicit conversion feature to implicitly convert at the end of the execution, calculating and returning the cumulative value
function addCurrying(){
let _args = Array.from(arguments);
let adapter = function(){
// Store the parameters of each execution method in closure variables_args.push(... arguments);return adapter
}
adapter.toString = function(){
return _args.reduce((pre,cur) = > pre+cur,0)}return adapter
}
Copy the code
Give it a try and it works!
addCurrying(1.2.3) (4); // f 10
addCurrying(1.2.3) (4) (5.6); // f 21
addCurrying(1.2.3) (4) (5.6) (7.8.9); // f 45
Copy the code