It seems like CoffeeScript automatically returns the last item in a scope. Can I avoid this functionality?
-
Why would you want to? Can't you just discard the returned value when you like?– ChrisSep 12, 2011 at 17:22
-
11@Chris: When there's no meaningful return value, leaking whatever happens to be the result of the last statement isn't a good idea.– user395760Sep 12, 2011 at 17:24
-
11@Chris this can have performance implications with comprehensions / for / while loops too, as coffeescript may collect the result of each iteration in an array, if the return value of the loop is used (or if it's the last expression of a function)– Arnaud Le BlancSep 12, 2011 at 17:34
-
1I just had this issue with a contructor. I defined a function in the last line of the constructor. So this was returned instead of my "Class-Function". I had to explicitly return this. Just saying, that this can be an issue.– MarkusMay 9, 2014 at 14:56
5 Answers
You have to explicitly return nothing, or to leave an expression evaluating to undefined at the bottom of your function:
fun = ->
doSomething()
return
Or:
fun = ->
doSomething()
undefined
This is what the doc recommends, when using comprehensions:
Be careful that you're not accidentally returning the results of the comprehension in these cases, by adding a meaningful return value — like true — or null, to the bottom of your function.
You could, however, write a wrapper like this:
voidFun = (fun) ->
->
fun(arguments...)
return
(Notice the splat operator here (...
))
And use it like this when defining functions:
fun = voidFun ->
doSomething()
doSomethingElse()
Or like this:
fun = voidFun(->
doSomething()
doSomethingElse()
)
-
Thank you for this informative answer. I too have been wondering about this as I'm presently learning coffeescript. I'm curious if you can shed some light on what the best choice, or what the worst choice would be when preventing the return of a value? Specifically, I have been told the options are to add either
return
,undefined
, ornull
to the end of my function. However, addingnull
seems wrong to me for some reason. Am I right to assume it the worst choice out of the three? Mar 29, 2012 at 20:32 -
2In javascript a function without return statement (or an empty return statement) returns undefined, so the best option is to either add an empty return or leave an
undfined
at the end. Using an emptyreturn
statement seems to express return nothing better that leaving anundefined
at the end of the function, so the emptyreturn
seems to be the better option. Dec 20, 2012 at 13:56 -
2Instead of
fun(arguments...)
, it would be better to callfun.apply(this, arguments)
. Aug 6, 2013 at 23:32 -
4Actually, "return" and "undefined" generate different javascript. Using "Try Coffeescript" at coffeescript.org, one can see that explicitly including "return" in coffeescript removes the return from the javascript; whereas, with "undefined", the javascript function ends with "return void 0;"– DanielMar 28, 2014 at 16:39
-
1I think you might need to be careful with voidFun because I suspect the inner function will still return whatever it wants which could possibly afaik cause the performance degradation.– AturSamsOct 27, 2014 at 7:08
Yes , with a return
as the last line of a function.
For example,
answer = () ->
42
extrovert = (question) ->
answer()
introvert = (question) ->
x = answer()
# contemplate about the answer x
return
If you'd like to see what js the coffee compiles to, look at this. (I've used coffeescript redux for my example)
-
1I think it was downvoted because this answer does not add any value apart from the one already there in the top-voted answer, which also happens to be given 2 years before this one! Jan 20, 2015 at 9:15
Just something fun(ctional)
suppressed = _.compose Function.prototype, -> 'do your stuff'
Function.prototype
itself is a function that always return nothing. You can use compose to pipe your return value into this blackhole and the composed function will never return anything.
It seems functions in CoffeeScript must always return something, even null
. In C, you have void
as a return type.
->
, the empty function, compiles to (function() {})
, so it's the only function that doesn't return anything.
-
That's not true.
return;
works in C, JavaScript and CoffeeScript to return a void value.– yynyNov 28, 2015 at 9:14