P Varga
P Varga

Reputation: 20229

Calling a function with no arguments in CoffeeScript

How do I call a function with no arguments in CoffeeScript without parens?

fun = -> alert "Boo!"

I have tried fun. and (fun)

Upvotes: 1

Views: 4395

Answers (2)

edi9999
edi9999

Reputation: 20544

You should write fun()

It is just like you do in javascript

If you write just fun in for example foo=fun , coffeescript will think it is just the fun variable (because there's no way to differentiate between a function call and a simple variable).

You can also use arguments inside the parenthesis, with fun(arg), but the "official syntax" when you call a function with arguments is fun arg

Upvotes: 11

metalim
metalim

Reputation: 1613

fun = -> alert "Boo!"
do fun

It is shorthand syntax for calling functions that are declared in-place.

someVar = do -> #some code...

Upvotes: 10

Related Questions