Javascript 速记三元运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8884071/
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
Javascript shorthand ternary operator
提问by Web_Designer
I know that in php 5.3 instead of using this redundant ternary operator syntax:
我知道在 php 5.3 中而不是使用这种冗余的三元运算符语法:
startingNum = startingNum ? startingNum : 1
...we can use a shorthand syntax for our ternary operators where applicable:
...我们可以在适用的情况下为我们的三元运算符使用速记语法:
startingNum = startingNum ?: 1
And I know about the ternary operator in javascript:
我知道 javascript 中的三元运算符:
startingNum = startingNum ? startingNum : 1
...but is there a shorthand?
...但是有速记吗?
回答by Brad Christie
var startingNumber = startingNumber || 1;
Something like that what you're looking for, where it defaults if undefined?
像你正在寻找的东西,如果未定义它默认在哪里?
var foo = bar || 1; // 1
var bar = 2;
foo = bar || 1; // 2
By the way, this works for a lot of scenarios, including objects:
顺便说一下,这适用于很多场景,包括对象:
var foo = bar || {}; // secure an object is assigned when bar is absent
回答by Adam Rackis
||
will return the first truthy value it encounters, and can therefore be used as a coalescing operator, similar to C#'s ??
||
将返回它遇到的第一个真值,因此可以用作合并运算符,类似于 C# ??
startingNum = startingNum || 1;
回答by Tadeck
Yes, there is:
就在这里:
var startingNum = startingNum || 1;
In general, expr1 || expr2
works in the following way (as mentioned by the documentation):
通常,expr1 || expr2
按以下方式工作(如文档所述):
Returns
expr1
if it can be converted totrue
; otherwise, returnsexpr2
.Thus, when used withBoolean
values,||
returnstrue
if either operand istrue
; if both arefalse
, returnsfalse
.
expr1
如果可以转换为true
,则返回;否则,返回expr2
。因此,当与Boolean
值一起使用时,如果任一操作数是,则||
返回;如果两者都是,则返回。true
true
false
false
回答by Daniel
var startingNum = startingNum || 1;
In this case, you can use the OR operator.
在这种情况下,您可以使用 OR 运算符。
回答by John Pace
The above answers are correct. In JavaScript, the following statement:
以上答案都是正确的。在 JavaScript 中,以下语句:
startingNum = startingNum ? otherNum : 1
can be expressed as
可以表示为
startingNum = otherNum || 1
Another scenario not covered here is if you want the value to return false when not matched. The JavaScript shorthand for this is:
此处未涵盖的另一种情况是,如果您希望该值在不匹配时返回 false。对此的 JavaScript 简写是:
startingNum = startingNum ? otherNum : 0
But it can be expressed as
但可以表示为
startingNum = startingNum && otherNum
Just wanted to cover another scenario in case others were looking for a more generalized answer.
只是想涵盖另一种情况,以防其他人正在寻找更普遍的答案。
回答by a2441918
startingNum = startingNum || 1
If you have a condition with null, like
如果您的条件为 null,例如
startingNum = startingNum ? startingNum : null
you can use '&&'
您可以使用 '&&'
startingNum = startingNum && startingNum
回答by xxjjnn
To make a ternary like:
制作一个三元像:
boolean_condition ? true_result : false_result
in javascript, you can do:
在 javascript 中,您可以执行以下操作:
(boolean_condition && true_result ) || false_result;
Example:
例子:
(true && 'green') || 'red';
=> "green"
(false && 'green') || 'red';
=> "red"