Javascript CoffeeScript 中的三元运算

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10146080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:04:00  来源:igfitidea点击:

Ternary operation in CoffeeScript

javascriptcoffeescriptternary-operator

提问by evfwcqcg

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

我需要根据条件设置值a

What is the shortest way to do this with CoffeeScript?

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

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

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

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

回答by loganfsmyth

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.

您可以在此处查看有关表达式示例的更多信息。

回答by Paul Oliver

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

See documentation.

请参阅文档

回答by Alexander Senko

In almost any language this should work instead:

在几乎任何语言中,这都应该起作用:

a = true  && 5 || 10
a = false && 5 || 10

回答by Max Peng

Coffeescript doesn't supportjavascript ternary operator. Here is the reason from the coffeescript author:

Coffeescript不支持javascript 三元运算符。这是咖啡脚本作者的原因:

I love ternary operators just as much as the next guy (probably a bit more, actually), but the syntax isn't what makes them good -- they're great because they can fit an if/else on a single line as an expression.

Their syntax is just another bit of mystifying magic to memorize, with no analogue to anything else in the language. The result being equal, I'd much rather have if/elsesalways look the same (and always be compiled into an expression).

So, in CoffeeScript, even multi-line ifs will compile into ternaries when appropriate, as will if statements without an else clause:

if sunny   
  go_outside() 
else   
  read_a_book().

if sunny then go_outside() else read_a_book()

Both become ternaries, both can be used as expressions. It's consistent, and there's no new syntax to learn. So, thanks for the suggestion, but I'm closing this ticket as "wontfix".

我和下一个人一样喜欢三元运算符(实际上可能更多一点),但语法并不是让它们变得更好的原因——它们很棒,因为它们可以将 if/else 放在一行中作为表达。

它们的语法只是一个需要记忆的神秘魔法,与语言中的其他任何东西都没有相似之处。结果是相等的,我宁愿if/elses总是看起来一样(并且总是被编译成一个表达式)。

因此,在 CoffeeScript 中,即使是多行 if 也会在适当的时候编译成三元,就像没有 else 子句的 if 语句一样:

if sunny   
  go_outside() 
else   
  read_a_book().

if sunny then go_outside() else read_a_book()

两者都变成三元组,都可以用作表达式。它是一致的,并且不需要学习新的语法。所以,感谢您的建议,但我将这张票关闭为“wontfix”。

Please refer to the github issue: https://github.com/jashkenas/coffeescript/issues/11#issuecomment-97802

请参考github问题:https: //github.com/jashkenas/coffeescript/issues/11#issuecomment-97802

回答by Alinex

You may also write it in two statements if it mostly is true use:

如果它主要是真正的用途,您也可以将其写成两个语句:

a = 5
a = 10 if false

Or use a switch statement if you need more possibilities:

如果您需要更多可能性,或者使用 switch 语句:

a = switch x
  when true then 5
  when false then 10

With a boolean it may be oversized but i find it very readable.

使用布尔值可能会过大,但我发现它非常可读。

回答by quotesBro

Multiline version (e.g. if you need to add comment after each line):

多行版本(例如,如果您需要在每行后添加注释):

a = if b # a depends on b
then 5   # b is true 
else 10  # b is false

回答by Эд Лесничий

CoffeeScripthas no ternary operator. That's what the docs say.

CoffeeScript没有三元运算符。这就是文档所说的。

You can still use a syntax like

您仍然可以使用类似的语法

a = true then 5 else 10

It's way much clearer.

就清晰多了