Written in front, this is I will begin to write a series, mainly in the framework of time, although USES the framework to work, but for the interview, and advanced technology, the basics of JS bedding is the icing on the cake, also had to learn a piece of knowledge, though the car does not need to be very good car, you just need to master automobile commonly used functions. But if you know cars, you can drive better, same thing. Of course, an article will not only talk about a knowledge point, generally will be linked to the knowledge point series, while recording their own learning, while sharing their own learning, mutual encourage! If possible, please also give me a like, your like can also make me work harder to update!
An overview of
- Serving time: 4-9 minutes
- Difficulty: Easy, don’t run, watch before you walk
Currie,
-
define
Instead of a function that takes n arguments, you have n nested functions that take only one argument. Instead of three arguments, you have three nested functions that take one argument at a time
-
implementation
This is a normal function that returns the province, city, and district
function getAddress(province,city,area){
return province + city + area;
}
getAddress(Zhejiang Province.'Hangzhou'.West Lake); // Xihu District, Hangzhou City, Zhejiang Province
Copy the code
Now, let’s transform it according to the above Currie definition, and the finished product is as follows:
function getAddress(province){
return function (city) {
return function (area) {
return province + city + area;
}
}
}
getAddress(Zhejiang Province) ('Hangzhou') (West Lake); // Xihu District, Hangzhou City, Zhejiang Province
Copy the code
The above approach has been curlified, as stated above, into n nested functions that take only one argument
-
meaning
Of course, learning to transform is only a part, the most important thing is to understand why this kind of transformation, for example, I am specifically responsible for the division of Hangzhou city, so, we in accordance with the first way in the front, when the new city division, the code should be like this.
getAddress(Zhejiang Province.'Hangzhou'.West Lake); // Xihu District, Hangzhou City, Zhejiang Province
getAddress(Zhejiang Province.'Hangzhou'.'Gongshu District'); // Gongshu District, Hangzhou City, Zhejiang Province
getAddress(Zhejiang Province.'Hangzhou'.'Uptown'); // Shangcheng District, Hangzhou City, Zhejiang Province
Copy the code
As you can see, hangzhou city, Zhejiang province is fixed, we have to pass in three parameters every time, and the first two parameters can be modified at will, I clearly only need to tube my Hangzhou things can be, so, after we carry out Curie transformation, you can use the following way to define
let city = getAddress(Zhejiang Province) ('Hangzhou');
city(West Lake); // Xihu District, Hangzhou City, Zhejiang Province
city('Gongshu District'); // Gongshu District, Hangzhou City, Zhejiang Province
city('Uptown'); // Shangcheng District, Hangzhou City, Zhejiang Province
Copy the code
As can be seen, according to the city() to create the city, will automatically bring “Hangzhou, Zhejiang province”, and no permission to operate provinces and cities do not need to operate provinces and cities, this is the charm of Curi
Partial function
-
define
Believe everyone through the interpretation of the above have basically understand the curry, so the partial function can well understood, it is “random” currie, say, ten parameters of function, after the modification and curry is a nested 10 layer at a time into a parameter function, after the modification and partial function, you can just fixed 3 into the participation, And then return a function that takes seven inputs, and the partial function doesn’t emphasize the concept of a single input as curryization does, and its goal is just to decompose the input into two parts, and it’s a little bit more arbitrary than Curryization.
-
implementation
Still the above example, now we carry out partial function transformation to it, the realization of the project needs to be still the same, I only need to manage my Hangzhou city division. Since only the urban area is responsible, the two input parameters can be fixed for the province and city. In this case, the first two input parameters can be fixed, and the last function that needs one input parameter can be returned. As can be seen, the partial function is not only “arbitrary” compared with currie, but also fixed the necessary correlation parameters according to the actual scenario, which is more suitable for the actual use scenario.
function getAddress(province,city){ return function (area) { console.log(province + city + area); }}Copy the code
And you can see, after we modified it, we changed it to pass in two parameters for the first time, and one parameter for the second time, and you can see that we didn’t intentionally modify it with one parameter, and that’s the partial function
Now, let’s go ahead and create the urban subdivision
let city = getAddress(Zhejiang Province.'Hangzhou'); city(West Lake); // Xihu District, Hangzhou City, Zhejiang Province city('Gongshu District'); // Gongshu District, Hangzhou City, Zhejiang Province city('Uptown'); // Shangcheng District, Hangzhou City, Zhejiang Province Copy the code
As you can see, Mr Currie and partial function of motivation is a function to “remember” part of the parameters, through the way of encapsulating, better service for practical usage scenarios, and encapsulation way, using all of these closures, if you still don’t understand what is the closure, you can refer to my article on this blog understand JS series (3) of the garbage collection mechanism, memory leaks, closure
Both of the above use closures to encapsulate passed parameters, two concepts that come up occasionally in interviews. In the next section, I’ll cover the use of the other two closures, damping and throttling
Series directory
-
This article understands JS series (a) compilation principle, scope, scope chain, variable promotion, temporary dead zone
-
This article understands JS series (2) JS memory life cycle, stack memory and heap memory, depth copy
-
JS series (3) to understand the garbage collection mechanism, memory leaks, closures
-
Understand JS series (four) closure application – Currie, partial function
-
Understand JS series (five) closure application – tremble, throttling
-
JS series (6) micro task and macro task, Event Loop
-
JS series (7) constructor, new, instance object, prototype, prototype chain, ES6 class
-
JS series (8) easy to understand the Promise
-
Learn more elegant asynchronous solutions in JS series (9)