有什么方法可以不使用 CoffeeScript 返回东西吗? [英] Is there any way to not return something using CoffeeScript?
问题描述
CoffeeScript 似乎会自动返回范围内的最后一项.我可以避免这个功能吗?
It seems like CoffeeScript automatically returns the last item in a scope. Can I avoid this functionality?
推荐答案
你必须明确返回什么,或者在你的函数底部留下一个评估为 undefined 的表达式:
You have to explicitly return nothing, or to leave an expression evaluating to undefined at the bottom of your function:
fun = ->
doSomething()
return
或者:
fun = ->
doSomething()
undefined
这是文档在使用推导时的建议:
This is what the doc recommends, when using comprehensions:
请注意,在这些情况下,通过添加有意义的返回值(如 true)或 null,到函数底部,不要意外返回推导结果.
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
(请注意此处的 splat 运算符(...代码>))
(Notice the splat operator here (...
))
并在定义函数时这样使用:
And use it like this when defining functions:
fun = voidFun ->
doSomething()
doSomethingElse()
或者像这样:
fun = voidFun(->
doSomething()
doSomethingElse()
)
这篇关于有什么方法可以不使用 CoffeeScript 返回东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!