Javascript currying vs method chaining - Stack Overflow

From what I have understood:Currying - functions returning functionsstring.capitalize(1)('characte

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 autopletion showing what methods you can use if you hit "dot" and it will show all the arguments you can pass.

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 autopletion showing what methods you can use if you hit "dot" and it will show all the arguments you can pass.

Share Improve this question edited Apr 25, 2022 at 15:33 Wicket 38.8k9 gold badges80 silver badges195 bronze badges asked Nov 8, 2010 at 3:08 ajsieajsie 79.9k110 gold badges284 silver badges387 bronze badges 1
  • 1 JavaScript does not support currying natively (it can be emulated with closures, but.. also, the example posted does not really look like a sensible curry :-) and thus just chaining (which is not orthogonal to currying) is normally used. A big "reason" to curry is to be able to use partially applied functions -- once again, doable just with closures. – user166390 Commented Nov 8, 2010 at 3:13
Add a ment  | 

1 Answer 1

Reset to default 9

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745160140a4614352.html

相关推荐

  • Javascript currying vs method chaining - Stack Overflow

    From what I have understood:Currying - functions returning functionsstring.capitalize(1)('characte

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信