我可以在内联 javascript if 语句中省略 else 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9232907/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:00:54  来源:igfitidea点击:

Can I omit the else in an inline javascript if statement?

javascript

提问by evan

I'm trying to use this and it doesn't appear to be working. I'm guessing it's just not an option, but want to confirm. Is this valid?

我正在尝试使用它,但它似乎不起作用。我猜这不是一种选择,而是想确认一下。这是有效的吗?

(if_it_is) ? thats_cool();

回答by Sarfraz

You can use &&there:

你可以&&在那里使用:

if_it_is && thats_cool();

It is basically equal to:

它基本上等于:

if (your_expression){
   thats_cool();
}

回答by Guffa

No, it's not valid.

不,它无效。

The conditional operator takes the form x ? y : z. The third operand is not like the elsein an if, it always has to be there.

条件运算符采用的形式x ? y : z。第三个操作数不像elsein if,它总是必须在那里。

回答by Marshall

What you are trying to use is a Ternary Operator. You are missing the else part of it.

您尝试使用的是Ternary Operator。你错过了它的其他部分。

You could do something like:

你可以这样做:

(if_it_is) ? thats_cool() : function(){};

or

或者

(if_it_is) ? thats_cool() : null;

Or, as others have suggested, you can avoid the ternary if you don't care about the else.

或者,正如其他人所建议的那样,如果您不关心 else,则可以避免使用三元。

if_it_is && thats_cool();

In JavaScript, as well as most languages, these logical checks step from left to right. So it will first see if if_it_isis a 'trusy' value(true, 1, a string other than '', et cetera). If that doesn't pass then it doesn't do the rest of the statement. If it does pass then it will execute thats_coolas the next check in the logic.

在 JavaScript 以及大多数语言中,这些逻辑检查从左到右进行。因此,它将首先查看是否if_it_is“trusy”值true, 1,除 之外的字符串等'')。如果那没有通过,则它不会执行其余的语句。如果它确实通过了,那么它将thats_cool作为逻辑中的下一个检查执行。

Think of it as the part inside of an if statement. Without the if. So it's kind of a shorthand of

将其视为 if 语句内部的部分。没有如果。所以这是一种简写

if (if_it_is && thats_cool()) { }

回答by driangle

You can't accomplish that using the ternary operator, but you canuse the short-circuit nature of &&to do what you want.

您无法使用三元运算符来实现这一点,但您可以使用 的短路特性&&来做您想做的事。

(if_it_is) && thats_cool();

回答by Niet the Dark Absol

No, you can't. ?:is a ternary operator and MUST have all three of its operands.

不,你不能。?:是一个三元运算符,并且必须具有它的所有三个操作数。

回答by Tomalak

No, you can't. It's not an "inline if statement", it's the ternary operator, and that is, well… ternary.

不,你不能。它不是“内联 if 语句”,而是三元运算符,也就是说,嗯……三元。

(if_it_is) ? thats_cool() : null;

Since you do not seem to be interested in the return value of the operation, you could just write:

由于您似乎对操作的返回值不感兴趣,您可以只写:

if (if_it_is) thats_cool();

though. That would also be better style than using a ternary.

尽管。这也比使用三元更好。

回答by Kath

You miss the third component. Should be something like that:

你错过了第三个组成部分。应该是这样的:

(if_it_is) ? thats_cool() : not_too_cool();

The whole sense is:

整个意义是:

condition ? do_something_if_condition_is_true : do_something_if_condition_is_false;