I have heard of currie function before, but once I heard this word I felt “good foreign style, it must be difficult”, this is my real idea when I saw this word, recently I focused on learning, learning “new knowledge” good cool ~
The concept of “Currie function”
In layman’s terms
When small function, need to use the first store these values, direct mechanism for use can be based on the scope chain, so we can say that the closure played a “store some value in advance, for the use of its context later at a lower level, we call this the thought of processing in advance, called” curry function”
A small example of a Curryized function
const fn = function (. params){
// Params is an array that stores the argument information passed by the outer function [ES6 residual operators]
// + duck function: params = [].slice.call(arguments)
return function proxy(. args) {
// args is an array that stores the argument information passed by the small function
/ / + params: [1, 2]
// +args:[3]
params = params.concat(args)
// Array summing 1: imperative programming [focus on process, own control of processing steps advantages: flexible disadvantages: complex redundancy]
let total = 0;
i = 0;
len =params.length;
for(; i<len; i++){ total+=params[i] }returntotal; }}let res = fn(1.2) (3)
console.log(res)
Copy the code
This is a perfect example of curryization if you understand it. Notice how the above example applies to currying
- Use function expressions to avoid variable promotion
- One application of closures is the Curryization function
- Classic closure application: large function execution produces a closure that stores the first argument passed.
In addition to the array summation in the example above, there are several other common array summation methods.
Functional programming
Functional programming: [focus on results, encapsulate specific implementation steps into a function, then only need to execute the function later, no need to implement the process] advantages: easy to use disadvantages: not flexible control => recommended to use functional programming
let total = 0;
params.forEach((item,index) = >{
total+=item;
})
return total;
Copy the code
“Cut corners”
return eval(params.join('+'))
Copy the code
Before introducing the fourth approach, let’s look at the Reduce API for arrays
reduce API
Reduce is also used to iterate over each item in the array, but it can pass the result of the last processing directly to the next processing, realizing the accumulation of each processing.
let arr = [10.20.30.40]
// First time: the first item in the total array, iterating through the array from the second item
arr.reduce((total,item,index) = >{
console.log(total,item,index)
/ / 10 20 1
30 30 / / 2
60 40 / / 3
// Reduce is the result of this process, and pass it to the next iteration
return total + item; / / = > 100
})
// If reduce passes a second value, then the initial value of total is the second value, and the array iterates from the first item...
arr.reduce(function(total,item,index){
console.log(total,item,index);
/ / 0 0; - > 10
/ / 10 20 1; - > 30
/ / 30 30 2; - > 60
// 60 40 3; - > 100
},0)
Copy the code
Reduce Array summation
// Array sum 4: reduce
return params.reduce((total,item) = >{
return total + item
},0)
Copy the code
The road ahead is long, but I see no ending. I will search up and down. Come on, let’s learn