Arn*_*anc 149
您必须显式返回任何内容,或者在函数底部留下一个未定义的表达式:
fun = ->
doSomething()
return
Run Code Online (Sandbox Code Playgroud)
要么:
fun = ->
doSomething()
undefined
Run Code Online (Sandbox Code Playgroud)
这是文档推荐的,当使用理解时:
在这些情况下,通过在函数的底部添加有意义的返回值(如true)或null,请注意不要意外地返回理解结果.
但是,您可以编写一个这样的包装器:
voidFun = (fun) ->
->
fun(arguments...)
return
Run Code Online (Sandbox Code Playgroud)
(注意splat运算符 here(...))
在定义函数时使用它:
fun = voidFun ->
doSomething()
doSomethingElse()
Run Code Online (Sandbox Code Playgroud)
或者像这样:
fun = voidFun(->
doSomething()
doSomethingElse()
)
Run Code Online (Sandbox Code Playgroud)
gpr*_*ant 10
是的,使用a return作为函数的最后一行.
例如,
answer = () ->
42
extrovert = (question) ->
answer()
introvert = (question) ->
x = answer()
# contemplate about the answer x
return
Run Code Online (Sandbox Code Playgroud)
如果你想看看咖啡编写的是什么,请尝试http://bit.ly/1enKdRl.(我的例子中使用过coffeescript redux)
只是有趣的事情(ctional)
suppressed = _.compose Function.prototype, -> 'do your stuff'
Run Code Online (Sandbox Code Playgroud)
Function.prototype本身就是一个永远不会返回的功能.您可以使用compose将返回值传递到此黑洞中,并且组合函数将永远不会返回任何内容.
| 归档时间: |
|
| 查看次数: |
33114 次 |
| 最近记录: |