I read this post on Dr. Dobb’s about currying and partial functions in JavaScript. It looks useful, but I’m wondering (as an occasional developer in JavaScript) if there are standard situations where this is regularly used?
I read this post on Dr. Dobb’s about currying and partial functions in JavaScript.
Share
For starters, I personally wouldn’t recommend currying in 99% cases. It can easily make code unreadable if used improperly.
However, some of the applications that I could name would be associated with setting the function context. For example when you first execute the currying function with a context (something other than window object etc), you could have a function that applies certain calculations on the original object’s properties etc when called later.
Second situation would be when, for example, you have a function that takes three arguments. The DOM element, the attribute name and it’s value. This could be turned into a currying function that you could initiate with the appropriate element, then each following execution would set an attribute to the value you wish. Might be useful in cases, where you have numerous conditionals on which the attributes depend.