Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 328
ajsie Avatar asked Nov 08 '10 03:11

ajsie


1 Answers

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.

like image 113
Stefan Kendall Avatar answered Oct 14 '22 13:10

Stefan Kendall