我遇到了很多情况,我想使用三元的条件作为其输出的实际值。有没有办法做到这一点而不必再次说出条件?
例如:
var thing = veryCoolThingExistsButItsNameIsVeryLong ? veryCoolThingExistsButItsNameIsVeryLong : otherThing;
我想要的是看起来更像这样的东西:
var thing = veryCoolThingExists?otherThing;
其中,仅当 veryCoolThingExists
不存在时,才会为 thing
分配 otherThing
。 p>
最佳答案
您可以使用逻辑或运算符,||
,
var thing = veryCoolThingExistsButItsNameIsVeryLong || otherThing;
如果为真则返回第一个值,否则返回第二个值。
关于javascript - 有没有办法使用三元的条件作为值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56757914/