javascript 简写 if 语句,没有 else 部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11554492/
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 if statement, without the else portion
提问by ahren
So I'm using a shorthand javascript if
/else
statement (I read somewhere they're called Ternary statements?)
所以我使用的是速记 javascript if
/else
语句(我在某处读到它们被称为三元语句?)
this.dragHandle.hasClass('handle-low') ? direction = "left" : direction = "right"
this.dragHandle.hasClass('handle-low') ? direction = "left" : direction = "right"
This works great, but what if later I want to use just a shorthand if
, without the else
portion. Like:
这很好用,但是如果以后我只想使用速记if
而不使用该else
部分怎么办。喜欢:
direction == "right" ? slideOffset += $(".range-slide").width()
direction == "right" ? slideOffset += $(".range-slide").width()
is this possible at all?
这可能吗?
回答by Andrey Sidorov
you can use &&
operator - second operand expression is executed only if first is true
您可以使用&&
运算符 - 仅当第一个为真时才执行第二个操作数表达式
direction == "right" && slideOffset += $(".range-slide").width()
in my opinion if(conditon) expression
is more readable than condition && expression
在我看来if(conditon) expression
比condition && expression
回答by Norguard
Don't think of it like a control-block (ie: an if-else or a switch). It's not really meant for running code inside of it.
不要将其视为控制块(即:if-else 或开关)。它并不是真正用于在其中运行代码。
You can. It just gets very ugly, very fast, which defeats the purpose.
你可以。它变得非常丑陋,非常快,这违背了目的。
What you really want to use it for is ASSIGNING VALUES.
您真正想要使用它的是ASSIGNING VALUES。
Taking your initial example and turning it on its head a little, you get:
以你最初的例子为例,稍微改变一下,你会得到:
direction = (this.dragHandle.hasClass("handle-low")) ? "left" : "right";
See. Now what I've done is I've taken something that would have required an if/else or a switch, which would have been used to assign to that one value, and I've cleaned it up nice and pretty.
看。现在我所做的是我已经采取了一些需要 if/else 或 switch 的东西,它们将被用来分配给那个值,并且我已经把它清理干净了。
You can even do an else-if type of ternary:
你甚至可以做一个 else-if 类型的三元:
y = (x === 2) ? 1 : (x === 3) ? 2 : (x === 4) ? 7 : 1000;
You can also use it to fire code, if you'd like, but it gets really difficult after a while, to know what's going where (see the previous example to see how even assignment can start looking weird at a glance)...
如果您愿意,您也可以使用它来触发代码,但是一段时间后,要知道发生了什么事情会变得非常困难(请参阅前面的示例以了解甚至赋值如何一目了然地开始看起来很奇怪)...
((this.dragHandle.hasClass("...")) ? fireMe(something) : noMe(somethingElse));
...this will typically work.
...这通常会起作用。
But it's not really any prettier or more-useful than an if or a branching, immediately-invoking function (and non-JS programmers, or untrained JS programmers are going to crap themselves trying to maintain your code).
但它并不比 if 或分支、立即调用函数更漂亮或更有用(非 JS 程序员,或未经培训的 JS 程序员会在试图维护您的代码时自欺欺人)。
回答by Guffa
The conditional operator is nota shorthand for the if
statement. It's an operator, not a statement.
条件运算符不是if
语句的简写。它是一个运算符,而不是一个语句。
If you use it, you should use it as an operator, not as a statement.
如果使用它,则应将其用作运算符,而不是用作语句。
Just use a zero value for the third operand:
只需对第三个操作数使用零值:
slideOffset += direction == "right" ? $(".range-slide").width() : 0;
回答by twiz
What you have will not work, but why not just use a one line if statement instead.
你所拥有的将不起作用,但为什么不使用一行 if 语句来代替。
if(direction == "right") slideOffset += $(".range-slide").width();
if(direction == "right") slideOffset += $(".range-slide").width();
This involves less typing than the method Ray suggested. Of course his answer is valid if you really want to stick to that format.
与 Ray 建议的方法相比,这涉及的输入更少。当然,如果你真的想坚持这种格式,他的回答是有效的。
回答by Yograj Gupta
No, This is not possible, because ternary operator requires, three operands with it.
不,这是不可能的,因为三元运算符需要三个操作数。
first-operand ? second-operand (if first evaluates to true) : third-operand (if false)
回答by Dale
This doesn't exactly answer your question, but ternaries allow you to write less than you've shown:
这并不能完全回答您的问题,但三元组允许您编写的内容少于您显示的内容:
direction = this.dragHandle.hasClass('handle-low') ? "left" : "right";
And now that I think about it, yeah, you can do your question too:
现在我考虑了一下,是的,你也可以提出你的问题:
slideOffset + direction == "right" ? = $(".range-slide").width() : = 0;
This is a theory. The next time I have an opportunity to +=
a ternary I will try this. Let me know how it works!
这是一个理论。下次我有机会去+=
三元我会试试这个。让我知道它是如何工作的!