最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript currying vs method chaining - Stack Overflow

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论