Javascript 三元运算符,其他为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31960619/
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 Ternary operator with empty else
提问by Probosckie
I'm trying to convert the following if-else to it's ternary operator representation in javascript as follows
我正在尝试将以下 if-else 转换为 javascript 中的三元运算符表示,如下所示
var x = 2;
if (x === 2) {alert("2");}
else
{ //do nothing}
But when I do this:
但是当我这样做时:
(t==2)?(alert("1")):();
Chrome throws a SyntaxError.
Chrome 抛出语法错误。
My question is - How to have a ternary operator in javascript with an empty "else" branch - i.e the part that comes after ":". Also, is this allowed- using a ternary operator in javascript to execute statements - NOT do assignment.
我的问题是 - 如何在带有空“else”分支的 javascript 中使用三元运算符 - 即“:”之后的部分。此外,这是否允许 - 在 javascript 中使用三元运算符来执行语句 - 不进行赋值。
Also: the above code was just a base case. I'm actually trying to get all the DOM elements of the page as an array (called all2) and then add only those elements to another array (called only) only if they have non-null class names. Here is my code:
另外:上面的代码只是一个基本情况。我实际上试图将页面的所有 DOM 元素作为一个数组(称为 all2)获取,然后仅将这些元素添加到另一个数组(仅调用)中,前提是它们具有非空类名。这是我的代码:
all2.forEach(function(e){ e.getAttribute("class") ? (only.push(e.getAttribute("class"))) : (); });
If I leave the third operand blank, it throws a syntax error. Passing a null works
如果我将第三个操作数留空,则会引发语法错误。传递一个空作品
回答by Nina Scholz
Answer to your real question in the comments:
在评论中回答您的真实问题:
all2.forEach(function (e) {
e.getAttribute("class") && only.push(e.getAttribute("class"));
});
回答by fra9001
You putted a lot of useless parentheses, and the best NULL value in js is undefined
.
你放了很多没用的括号,js中最好的NULL值是undefined
.
document.getElementById('btn-ok').onclick = function(){
var val = document.getElementById('txt-val').value;
val == 2 ? alert(val) : undefined;
}
<input id="txt-val" type="number" />
<button type="button" id="btn-ok">Ok</button>
using a single line if
statement is better though
if
虽然使用单行语句更好
if(value === 2) alert(value);
回答by potatopeelings
Do this :
做这个 :
(t==2)?(alert("1")):null;
You could replace null by any expression that has no side effect. () is not a valid expression.
您可以用任何没有副作用的表达式替换 null。() 不是有效的表达式。
回答by MoLow
you have a few options to do this nicely in one line:
你有几个选项可以在一行中很好地做到这一点:
option1 - noop function
选项 1 - noop 函数
set a global noop function:
设置一个全局 noop 函数:
function noop(){}
(t==2)?(alert("1")):(noop());
option2 - && operator
选项 2 - && 运算符
when you use && operater, operands are evaluted only if previos ones where true, so you could miply write:
当您使用 && 运算符时,仅当先前的操作数为真时才计算操作数,因此您可以简单地编写:
(t==2) && alert("1");
or, for exapmle if you have an arry you want to push to, you could test it is not null before:
或者,例如,如果您有一个要推送到的 arry,您可以测试它之前是否为空:
arr && arr.push(obj)
回答by Milos Miskone Sretin
In that case you don't need to use Ternary operator. Ternary operator requires a third argument.
在这种情况下,您不需要使用三元运算符。三元运算符需要第三个参数。
condition ? expr1 : expr2
状况 ?表达式 1 : 表达式 2
Lokki at Conditional (ternary) Operator
条件(三元)运算符的Lokki
You can use the if statement
您可以使用if 语句
if ( t == 2 ) alert(1);
回答by venkat7668
NO, you can't have empty else, better don't use the ternary operator, it requires a third argument. Go with simple if condition.
不,你不能有空的 else,最好不要使用三元运算符,它需要第三个参数。使用简单的 if 条件。
if(t==2) alert("2");
回答by James M
I don't like it's either. So you're on the right track looking for alternatives.
我也不喜欢。所以你正在寻找替代品的正确轨道上。
In this case, I would write:
在这种情况下,我会写:
t===2 && alert("2")
Your idea is valid too, for instance you can do this:
你的想法也是有效的,例如你可以这样做:
t===2 ? alert("2") : null
But it's four extra chars.
但它是四个额外的字符。