gpt4 book ai didi

coffeescript - 有什么方法可以使用 CoffeeScript 不返回某些内容吗?

转载 作者:行者123 更新时间:2023-12-03 04:39:03 25 4
gpt4 key购买 nike

CoffeeScript 似乎自动返回范围中的最后一项。我可以避免这个功能吗?

最佳答案

您必须显式返回任何内容,或者在函数底部保留一个计算结果为未定义的表达式:

fun = ->
doSomething()
return

或者:

fun = ->
doSomething()
undefined

这是文档在使用推导式时的建议:

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.

<小时/>

但是,您可以编写如下包装器:

voidFun = (fun) ->
->
fun(arguments...)
return

(注意此处的 splat operator (...))

在定义函数时像这样使用它:

fun = voidFun ->
doSomething()
doSomethingElse()

或者像这样:

fun = voidFun(->
doSomething()
doSomethingElse()
)

关于coffeescript - 有什么方法可以使用 CoffeeScript 不返回某些内容吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7391493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com