CoffeeScript 中的三元运算

我需要根据条件为 a 设置值.

I need to set value to a that depends on a condition.

使用 CoffeeScript 执行此操作的最短方法是什么?

What is the shortest way to do this with CoffeeScript?

例如这就是我在 JavaScript 中的做法:

E.g. this is how I'd do it in JavaScript:

a = true  ? 5 : 10  # => a = 5
a = false ? 5 : 10  # => a = 10

推荐答案

由于一切都是表达式,因此会产生一个值,因此您可以使用 if/else.

Since everything is an expression, and thus results in a value, you can just use if/else.

a = if true then 5 else 10
a = if false then 5 else 10

您可以在这里查看更多关于表达式示例的信息.

You can see more about expression examples here.

相关文章