Search code examples
javascriptcurryingmethod-chaining

Javascript currying vs method chaining


From what I have understood:

Currying - functions returning functions

string.capitalize(1)('character')('at the end')

Method chaining - methods returning objects

string.lowercase.capitalize.uppercase

Is this understanding correct?

If so, are there cases one of them is better than the other?

Cause it seems to me that method chaining is better and more readable. You also have autocompletion showing what methods you can use if you hit "dot" and it will show all the arguments you can pass.


Solution

  • A better equivalent of currying would be the Builder design pattern.

    Ergo, you would do something like:

    myObject.setIndexRangeToEffect(1,1).setTextTransformation(UPPERCASE).execute();

    At any point before calling execute, you essentially have a "curried" action object.