Coffeescript 中的条件运算符
我真的很喜欢这个:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
javascript 和 Coffeescript 中都有一个更简洁的选项:)
There is a more concise option in both javascript and coffeescript :)
正如 Răzvan Panda 指出的那样,我的评论实际上可能是更好的答案之一:
As Răzvan Panda points out, my comment may actually one of the better answers:
在这种情况下,感觉 CoffeeScript 具有相互竞争的理念:
由于所有操作都会返回结果,因此 if/then/else 的处理方式可以满足您的需要。添加 ?/: 运算符是多余的。
这就是我希望他们给我们 ?/: 三元运算符的地方,即使它是多余的......它只是比 if/then/else 变体读起来更好。
只是我的2c。
This is a case where it feels like CoffeeScript has competing philosophies:
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.
你可以这样写:
它将像你的代码一样编译。
You can write it like this:
It will compile like your code.
事实如下:
在文档中,有一个标题为“条件、三元和条件赋值”的部分。这让人相信 CoffeeScript 支持
,但实际上并不支持。
以下是有关解决此问题的补丁的信息
这是补丁(它已推送到 coffeescript.org ):
http://github.com/jashkenas/coffee-script/commit/ec2d358ae3c82e9888c60695d7cce05edde0c55a
示例:
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
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:
这实际上是不正确的,请检查评论。
This is actually not correct, check the comments.