CoffeeScript 中的三元运算 [英] Ternary operation in CoffeeScript

查看:19
本文介绍了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

您可以在这里查看更多关于表达式示例的信息.

You can see more about expression examples here.

这篇关于CoffeeScript 中的三元运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆