Coffeescript 中的条件运算符

发布于 2024-12-16 20:41:51 字数 134 浏览 7 评论 0原文

我真的很喜欢这个:

var value = maxValue > minValue ? minValue : maxValue;

Coffescript 中是否有同样简洁的东西?

I really like this:

var value = maxValue > minValue ? minValue : maxValue;

Is there something equally concise in Coffescript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

诗化ㄋ丶相逢 2024-12-23 20:41:51
value = if maxValue > minValue then minValue else maxValue
value = if maxValue > minValue then minValue else maxValue
生活了然无味 2024-12-23 20:41:51

javascript 和 Coffeescript 中都有一个简洁的选项:)

value = Math.min(minValue, maxValue)

There is a more concise option in both javascript and coffeescript :)

value = Math.min(minValue, maxValue)
七禾 2024-12-23 20:41:51

正如 Răzvan Panda 指出的那样,我的评论实际上可能是更好的答案之一:

value = `maxValue > minValue ? minValue : maxValue`

As Răzvan Panda points out, my comment may actually one of the better answers:

value = `maxValue > minValue ? minValue : maxValue`
凑诗 2024-12-23 20:41:51

在这种情况下,感觉 CoffeeScript 具有相互竞争的理念:

  1. 简洁
  2. 不要冗余

由于所有操作都会返回结果,因此 if/then/else 的处理方式可以满足您的需要。添加 ?/: 运算符是多余的。

这就是我希望他们给我们 ?/: 三元运算符的地方,即使它是多余的......它只是比 if/then/else 变体读起来更好。

只是我的2c。

This is a case where it feels like CoffeeScript has competing philosophies:

  1. Be concise
  2. Don't be redundant

Since all operations return a result, the if/then/else way of doing things gives you what you need. Adding the ?/: operator is redundant.

This is where I wish they'd give us the ?/: ternary operator even though it is redundant... it simply reads better than the if/then/else variant.

Just my 2c.

柏拉图鍀咏恒 2024-12-23 20:41:51

你可以这样写:

value = if maxValue > minValue then minValue else maxValue

它将像你的代码一样编译。

You can write it like this:

value = if maxValue > minValue then minValue else maxValue

It will compile like your code.

安人多梦 2024-12-23 20:41:51

事实如下:

在文档中,有一个标题为“条件、三元和条件赋值”的部分。这让人相信 CoffeeScript 支持

condition ? when-true : when-false 

,但实际上并不支持。

以下是有关解决此问题的补丁的信息

这是补丁(它已推送到 coffeescript.org ):

http://github.com/jashkenas/coffee-script/commit/ec2d358ae3c82e9888c60695d7cce05edde0c55a

示例:

mood = greatlyImproved if singing

if happy and knowsIt
  clapsHands()
  chaChaCha()
else
  showIt()

date = if friday then sue else jill

options or= defaults

Below is the fact:

In the documentation, there's a section titled "Conditionals, Ternaries, and Conditional Assignment". This leads one to believe that coffeescript supports

condition ? when-true : when-false 

but in fact it does not.

Below is the information about the patch which will solve this issue

Here's the patch (and it's pushed to coffeescript.org):

http://github.com/jashkenas/coffee-script/commit/ec2d358ae3c82e9888c60695d7cce05edde0c55a

Examples:

mood = greatlyImproved if singing

if happy and knowsIt
  clapsHands()
  chaChaCha()
else
  showIt()

date = if friday then sue else jill

options or= defaults
灼痛 2024-12-23 20:41:51
value = maxValue > minValue && minValue || maxValue

这实际上是不正确的,请检查评论。

value = maxValue > minValue && minValue || maxValue

This is actually not correct, check the comments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文