JavaScript 中的条件运算符
像这样的语句可以使用条件运算符吗?
(x == y) ? alert("yo!") : alert("meh!");
或者使用它来分配像这样的值更正确?
z = (x == y) ? "yo!" : "meh!";
如果像语句一样使用它并没有错误,那么是否可以像这样添加多行代码来执行? 对于多行代码使用 ifthen 和 switch 语句是否更正确?
(x == y) ? (alert("yo!"), document.write("woot!")) : (alert("meh!"), document.write("blah!"));
Is it ok to use conditional operators like a statement like so?
(x == y) ? alert("yo!") : alert("meh!");
Or is it more correct to use it to assign a value like so?
z = (x == y) ? "yo!" : "meh!";
If it's not incorrect to use it like a statement, then is it possible to add more than one line of code for execution like so? Is it more correct to use ifthen and switch statements for multiple lines of code?
(x == y) ? (alert("yo!"), document.write("woot!")) : (alert("meh!"), document.write("blah!"));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
条件运算符有意简洁,对于赋值特别有用:
使用它们有条件地运行函数,虽然可能,但为了可读性,应该使用 IF/ELSE 语句来完成:
虽然冗长,但大多数时候,这是更好的解决方案:
IF/ELSE 结构的一个显着好处是,您可以在每种条件下轻松添加额外的任务。
您的最后一个片段也是可能的,但它看起来有点冗长,而且可能更适合更传统的逻辑结构; 就像 IF/ELSE 块一样。
也就是说,条件运算符仍然是可读的,例如
最后,像许多事情一样,这取决于个人喜好。 永远记住,您正在编写的代码不仅仅适合您;也适合您。 其他开发者将来可能不得不费力地经历它; 尝试使其尽可能具有可读性。
Conditional operators are intentionally succinct and especially useful for assignments:
Using them to conditionally run functions, while possible, should, for the sake of readability be done using IF/ELSE statements:
While long-winded, most of the time, this is the better solution:
One notable benefit to the IF/ELSE structure is that you can add additional tasks under each condition with minimal hassle.
Your last snippet is also possible but it looks somewhat long-winded and, again, might be better suited to a more conventional logical structure; like an IF/ELSE block.
That said, a conditional operator can still be readable, e.g.
In the end, like many things, it's down to personal preference. Always remember that the code you're writing is not just for you; other developers might have to wade through it in the future; try and make it as readable as possible.
JavaScript 不会阻止您这样做,但这是一种非常不寻常的做法,会让任何阅读您代码的人感到困惑。
条件运算符几乎总是用于选择两个替代值,而不是语句。 对于语句的条件分支,首选
if
语句。至于你的最后一个问题,是的,如果你确实必须这样做,你可以滥用
[]
结构:但请不要这样做。 8-)
JavaScript won't prevent you from doing it, but it's very a unusual practice that will confuse anyone reading your code.
The conditional operator is almost always used for selecting two alternative values, not statements. An
if
statement is preferred for conditional branching of statements.As to your last question, yes, if you really must, you can abuse the
[]
construct:But please don't. 8-)
这完全取决于你,你可以选择任何一种方式。 不过,您只需要问自己,这种风格是否遵循公司准则,以及您希望此代码的可读性如何?
使用 if 语句更具可读性。
就我个人而言,我仅将三元运算符用于简单且快速的真/假条件 - 这样做有意义 - 或者我需要“内联”的东西。
It is entirely up to you, you can do it either way. You just have to ask yourself, though, does this style follow company guidelines, and how readable do you want this code to be?
Using if statements is way more readable.
Personally, I only use the ternary operator for simple and quick true/false conditions--where doing so makes sense--or where I need something "inline".
这两种方法中的任何一种都是可以接受的,尽管您也可以编写:
除此之外,我永远不会建议对多行语句使用内联条件,只需使用标准的 if/else 块。 鉴于您输入的语法也不是有效的 JS,您可以将多个语句放入匿名方法和 yada yada 中,然后您就会陷入几乎无法管理且不必要的困难代码的混乱之中。 再说一遍,标准 if/else。
Either of the two methods are acceptable although you could have also written:
Aside from that I would never recommend using an inline conditional for multiline statements, just use a standard if/else block. Given the syntax you entered isn't valid JS either, you could have placed the multiple statements into anonymous methods and yada yada, then you enter into a tangled mess of nearly unmanageable and unnecessarily difficult code. So again, standard if/else.
我同意 Chris 和 JP 的观点:
var a = x ? 1 : 2;
if/else
语法。 可读性是为了满足读者的期望,因此熟悉度很重要。我要补充的是,多行条件运算符会让您容易出现分号插入错误。 请查看 JSLint 文档(请参阅“换行”部分)以了解更多信息。 如果必须使用多行条件运算符,请确保运算符位于每行的末尾。 我将因此修改 JP 的多行示例:
正如已经提到的,有很多样式指南,您可以选择您喜欢的任何一个。 然而,有些选择比其他选择更容易出错。 一定要仔细查看 JSLint 文档; 它是一个经过深思熟虑的风格指南,如果您遵守它,您甚至可以使用 JSLint 工具自动检查代码是否存在潜在问题。
I agree with both Chris and J-P that:
var a = x ? 1 : 2;
if/else
syntax is much more familiar to most developers. Readability is about matching the expectations of your reader, so familiarity is important.I will add that multi-line conditional operators leave you open to semicolon insertion errors. Check out the JSLint documentation (see the section on "Line Breaking") for more on this. If you must use a multi-line conditional operator, make sure operators are at the end of each line. I would rework J-P's multi-line example thusly:
As has been mentioned, there are many style guides out there and you can choose whichever you prefer. Some choices are more error-prone than others, however. Definitely take a close look at that JSLint documentation; it is a very well-thought-out style guide, and if you adhere to it, you can even use the JSLint tool to automatically check your code for potential problems.