有没有一个更简洁的Coffeescape习语来形容这个

Is there a more concise Coffeescript idiom for this?

本文关键字:习语 Coffeescape 有一个 简洁      更新时间:2023-09-26

我正在尝试翻译这个Javascript代码:

if(error) {
    foo();
    return null;
}
bar()

转换为Coffeescapet代码,其中bar()是另一段长代码,我不想缩进更多

还有比这更好的方法吗?

if error
    foo()
    return null
bar()

您的代码对我来说很好。

不过,如果你真的想让它成为一行,你可以利用括号:

return (foo(); null) if error

怎么样

if error
    foo()
else
    bar()

(可能带有尾部return

您可以用另一种形式重写它(在符号上略有不同,因为您返回的是foo()的结果):

return foo() if error