Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the curry of Js function

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to realize the curry of Js function". In the daily operation, I believe that many people have doubts about how to realize the curryization of Js function. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to realize curry of Js function". Next, please follow the editor to study!

With regard to function curryalization, it is actually similar to the idea of chained calls. By constantly returning functions, multiple'(... args) 'can be executed to return results at an appropriate time.

The implementation of function curryization:

Curryization of generalization

Letsum=function () {

Vartotal=0

For (leti=0,c;c=arguments [iTunes +];) {

Total+=c

}

Returntotal

}

Letcurry= (fn) = > {

Letargs= []

/ / instead of using the callee attribute, call the function name directly.

Returnfunctionnext () {

If (arguments.length===0) {

Returnfn.apply (this,args)

}

/ / the function after curryization is used by constantly collecting parameters and making a no-parameter call to make the final calculation after the parameters are collected.

/ / so the function of curryization here is to collect parameters. Of course, different curry functions can achieve different results.

Args= [... args,...arguments]

/ / returnarguments.callee

Returnnext

}

}

Letsum_curry1=curry (sum)

Sum_curry1 (1) (2pr 3)

Sum_curry1 (4)

Console.log (sum_curry1 ())

The function of curry

Improve applicability

[general function] solves the compatibility problem, but it will also come back at the same time, because of the inconvenience of use, different application scenarios, you have to pass a lot of parameters to achieve the purpose of solving specific problems. Sometimes in application, the same rule may be used repeatedly, which may result in code repetition.

Look at the following example:

Functionsquare (I) {

Returni*i

}

Functiondubble (I) {

Returni*=2

}

Functionmap (handeler,list) {

Returnlist.map (handeler)

}

At this point, the study of "how to curry the Js function" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report