Reputation: 20229
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
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
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