在CoffeeScript中的三元操作 [英] Ternary operation in CoffeeScript
本文介绍了在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
详情请参阅这里。
这篇关于在CoffeeScript中的三元操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文