To get a good understanding of JavaScript closures before reading this blog, take a look at what I wrote earlierJavaScript: Closures

I assume you’re reading this blog post knowing closures.

I don’t want to do anything. I don’t want to learn anything. I don’t want to play games. Ah.. But recently I saw a domestic animated film nezha, I feel that the level of domestic animation is getting higher and higher. It is very good and worth watching

What is the Currization of a function

Use of function currying, or making a function taste like curry. In fact, when I first heard of this term, I thought it was really nice, but after reading some books (JavaScript Advanced Programming) and some blogs written by my predecessors, IT turns out that function corrification is using closures to return a function

A Currified function can delay receiving arguments, so for example, if I have a function that needs to receive two arguments, I have to receive two arguments when I execute it, otherwise I can’t execute it, right? That’s a problem. However, a Currified function can accept a function first, and then a function, which is too blunt to say, so let’s look at a simple example (and the most common example on the web)⬇️

This is a normal function

// This is what happens when we declare functions
functionFind the sum of two numbers (First term, second term) {
  returnThe first term plus the second term}constAnd = find the sum of two numbers (1.2);
console.log(The sum of the two numbers is zeroThe ${and}`); / / 3
Copy the code

// Understand the idea of Corrification
functionFind the sum of two numbers (The first number) {
  return function (The second number) {
    returnFirst term + second term}}// The first method
constAnd = find the sum of two numbers (1) (2);
console.log(The sum of the two numbers in the first way is zeroThe ${and}`);
// The second method
constThe summation function of the first received number =1);
constThe sum of two received numbers = the sum function of the first received number (2);
console.log(The sum of the two numbers in the second way is zero${sum where both numbers are received}`);
Copy the code

From the above comparison, we can see that the Currified function can first take a function, and then pass in the second parameter when we need it, and execute the final result.

So we can dynamically generate functions where the first argument is the same and the second argument is different, or the second argument is the same and the first argument is different

The currization of a function

In the familiar JavaScript Advanced Programming, there is a method that allows us to convert a function that does not support The idea of Currization into a function that supports currization.

functionCurrize the function (It needs to be currified) {
  constThe argument other than function = when calledArray.prototype.slice.call(arguments.1);
  return function () {
    constArguments accepted after = [...arguments];constFinal arguments = [... arguments other than the function when calling the Cremation, arguments accepted after...];returnFunctions that require currization. Apply (null, final parameters); }}functionFind the sum of two numbers (First term, second term) {
  returnThe first term plus the second term}constTo take the sum of two numbers = to take the sum of two numbers,1);

console.log(
  The sum of the two numbers of the Currized function is zeroFind the sum of two numbers (${Currie)2)}`
); // The sum of the two numbers is 3
Copy the code

We found that we could write a function, and then convert an ordinary function into a function that supports Currization, but this method, provided by high-level programming, only provides two calls. What does that mean? How does the Curry method work in LoDash

const _ = require('lodash');

functionFind the sum of three numbers (Number one, number two, number three) {
  returnFirst term + second term + third term}const= _. Curry (sum of three);console.log(The sum of the three numbers (1) (2) (3)); / / 6
console.log(The sum of the three numbers (1) (2.3)); / / 6
console.log(The sum of the three numbers (1.2.3)); / / 6
Copy the code

So, what we see here is that in LoDash, we can call any function that has been currified, no matter how many arguments we pass, as long as we pass three of them, no matter how many times we pass them.

Let’s do a Currie function of our own

What are our needs?

feature

  1. We need to convert ordinary functions into Currie functions
  2. As long as the number of arguments does not reach the converted function, the function is returned, saving the passed arguments
  3. Supports unlimited calls when the number of arguments to be converted has not reached

Let’s first look at the function we need to convert ⬇️

constGranting titles to gods =(Name, treasure, place of birth) = > console.log(I was `${place of birth}theThe ${name}And I haveThe treasures of the ${}`); Granting titles to gods ('which zha'.'The Universe'.'Chan Tong Pass'); // I am Ne Zha of Chentangguan. I have the Universe
Copy the code

What is the result of our ordinary transformation ⬇️

constEnshrine = Name => Treasure => Birthplace =>console.log(I was `${place of birth}theThe ${name}And I haveThe treasures of the ${}`); Granting titles to gods ('which zha') ('The Universe') ('Chan Tong Pass');
Copy the code

Ordinary transformation function, we pass the parameter is not very free, must wear three times, each pass one, then we come to a cow force ⬇️

constGranting titles to gods =(Name, treasure, place of birth) = > console.log(I was `${place of birth}theThe ${name}And I haveThe treasures of the ${}`);

constCurry =(A function that requires currization... parameter) = >The function that needs curryization. Length <= argument. Length? Functions that require currization (... Parameter) :(. More parameters) = >Currization (the function that needs currization,... Parameters,... More parameters);constCremation = Cremation (cremation); Currie's canonization list ('which zha'.'The Universe'.'Chan Tong Pass'); // I am Ne Zha of Chentangguan. I have the UniverseCurrie's canonization list (Sun Wukong) ('Gold hoop robust') ('Flower and Fruit Hill'); // I am the Monkey King of Flower and Fruit Mountain, AND I have a strong gold hoopCurrie's canonization list ('thundershock'.'Golden stick') ('Zhongnan Mountain Yuzhu Cave'); // I am the thunder of Zhongnan Mountain yuzhu cave, I have a gold stick()()()()()()()()()()'Ginger tooth'.'Strike the whip'.'Kunlun Mountains'); // I am the ginger tooth of kunlun Mountain, I have the whip of god
Copy the code

Look, now we’ve implemented a Currization function that basically meets the requirements we just mentioned

Of course, there is a Collerization there is anti-Collerization, but I think it is not necessary, since I have already collerization, collerization of the function of course support non-Collerization function of the way of passing parameters, but interested partners can still understand

conclusion

In fact, the concept of a function called Coriolization, we may not have heard of it before, but we may have been more or less exposed to it in our work or study, but we didn’t know that it was called Coriolization, that this function was called a Coriolization function.

The Method of Currie function is very practical, in the daily development process will still encounter the need for this requirement.


I’m a front end warrior, a front end elementary school.