Daryna
Daryna

Reputation: 759

return a function without params in coffeescript using do

I want to use do to call a function without parameters like so:

do someMethod

from experimenting it seems like I can also write:

total = 42
do total.toString

question, can I do this:

return do next if request.someField == 'ok'

instead of:

return next() if request.someField == 'ok' 

Upvotes: 2

Views: 226

Answers (1)

Linus Thiel
Linus Thiel

Reputation: 39223

The answer is yes.

I see you are asking about convention. The convention, AFAIK, is to use parentheses when there are no parameters: foo(), and to use do when it's convenient to wrap a few variables in a closure:

for item, index in items
  do (item, index) ->
    // Do some stuff

Upvotes: 1

Related Questions