Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

javascript - CoffeeScript中的三元操作(Ternary operation in CoffeeScript)

I need to set value to a that depends on a condition.(我需要设置值, a是取决于一个条件。)

What is the shortest way to do this with CoffeeScript?(使用CoffeeScript执行此操作的最短方法是什么?)

Eg this is how I'd do it in JavaScript:(这就是我在JavaScript中的表现:)

a = true  ? 5 : 10  # => a = 5
a = false ? 5 : 10  # => a = 10
  ask by evfwcqcg translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since everything is an expression, and thus results in a value, you can just use if/else .(由于一切都是表达式,因此产生一个值,你可以使用if/else 。)

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

You can see more about expression examples here .(您可以在此处查看更多关于表达式示例)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...