Javascript 使用 if-else 速记时省略第二个表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11069278/
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
Omitting the second expression when using the if-else shorthand
提问by Nikki
Can I write the if else
shorthand without the else
?
我可以在if else
没有 的情况下写速记else
吗?
var x=1;
x==2 ? dosomething() : doNothingButContinueCode();
I've noticed putting null
for the else works (but I have no idea why or if that's a good idea).
我注意到null
为 else 工作(但我不知道为什么或者这是否是个好主意)。
Edit:Some of you seem bemused why I'd bother trying this. Rest assured it's purely out of curiosity. I like messing around with JavaScript.
编辑:你们中的一些人似乎对我为什么要费心尝试这个感到困惑。请放心,这纯粹是出于好奇。我喜欢玩弄 JavaScript。
采纳答案by ajax333221
This is also an option:
这也是一种选择:
x==2 && dosomething();
dosomething()
will only be called if x==2
is evaluated to true. This is called Short-circuiting.
dosomething()
只有在x==2
评估为真时才会调用。这称为短路。
It is not commonly used in cases like this and you really shouldn't write code like this. I encourage this simpler approach:
它在这种情况下并不常用,您真的不应该编写这样的代码。我鼓励这种更简单的方法:
if(x==2) dosomething();
You should write readable code at all times; if you are worried about file size, just create a minified version of it with help of one of the many JS compressors. (e.g Google's Closure Compiler)
您应该始终编写可读的代码;如果您担心文件大小,只需在众多 JS 压缩器之一的帮助下创建它的缩小版本。(例如 Google 的Closure Compiler)
回答by Nicole
What you have is a fairly unusual use of the ternary operator. Usually it is used as an expression, not a statement, inside of some other operation, e.g.:
您所拥有的是三元运算符的一种相当不寻常的用法。通常它在其他一些操作中用作表达式,而不是语句,例如:
var y = (x == 2 ? "yes" : "no");
So, for readability (because what you are doing is unusual), and because it avoids the "else" that you don't want, I would suggest:
因此,为了可读性(因为您正在做的事情不寻常),并且因为它避免了您不想要的“其他”,我建议:
if (x==2) doSomething();
回答by Buzinas
Another option:
另外一个选项:
x === 2 ? doSomething() : void 0;
回答by Prescott
If you're not doing the else, why not do:
如果你不做其他,为什么不做:
if (x==2) doSomething();
回答by Ted Hopp
Using null
is fine for one of the branches of a ternary expression. And a ternary expression is fine as a statement in Javascript.
null
对于三元表达式的分支之一,使用是很好的。并且三元表达式可以作为 Javascript 中的语句。
As a matter of style, though, if you have in mind invoking a procedure, it's clearer to write this using if..else:
不过,就风格而言,如果您想调用一个过程,使用 if..else 编写它会更清楚:
if (x==2) doSomething;
else doSomethingElse
or, in your case,
或者,就你而言,
if (x==2) doSomething;
回答by DotBot
A tiny addition to this very, very old thread..
这个非常非常古老的线程的一个小补充..
Let's say your'e inside a for
loop and need to evaluate a variable for a truthy/falsy
value with a ternary operator, while in case it's falsy
you want to continue
- you gonna have a problem because your'e not returning an expression,you return instead a statementwithout any value.
假设你在一个for
循环中并且需要truthy/falsy
用三元运算符评估一个变量的值,而万一falsy
你想要continue
- 你会遇到问题,因为你没有返回一个表达式,你返回一个没有表达式的语句任何值。
This will produce Uncaught SyntaxError: Unexpected token continue
这将产生 Uncaught SyntaxError: Unexpected token continue
for (var i = 0; i < myArray.length; i++) {
myArray[i].value ? alert('yay') : continue;
}
So, if you do want to return a statement and still use one line for your code, while it may seem kinda weird at first glance and may not follow strict language usage, you can do this instead:
所以,如果你确实想返回一个语句并且仍然使用一行代码,虽然乍一看似乎有点奇怪并且可能不遵循严格的语言用法,你可以这样做:
for (var i = 0; i < myArray.length; i++) {
if (myArray[i].value) alert('yay') ; else continue;
}
- P.S - This code may be hard to read or understand so it will not always be the best option to use. Just saying.. :)
- PS - 此代码可能难以阅读或理解,因此它并不总是使用的最佳选择。只是说.. :)
回答by nhahtdh
Technically, putting null or 0, or just some random valuethere works (since you are not using the return value). However, why are you using this construct instead of the if
construct? It is less obvious what you are trying to do when you write code this way, as you may confuse people with the no-op (null in your case).
从技术上讲,将 null 或 0 或只是一些随机值放在那里是可行的(因为您没有使用返回值)。但是,为什么要使用此构造而不是if
构造?当您以这种方式编写代码时,您尝试执行的操作不太明显,因为您可能会将人们与无操作(在您的情况下为 null)混淆。
回答by Kamil Kie?czewski
Probably shortest (based on OR operatorand its precedence)
x-2||dosomething()
let x=1, y=2;
let dosomething = s=>console.log(s);
x-2||dosomething('x do something');
y-2||dosomething('y do something');