javascript 如何在Javascript中编写没有'else'的IF else语句

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

How to write an IF else statement without 'else' in Javascript

javascript

提问by code511788465541441

I can't use elsein my script; how can I implement an if elsestatement without using else?

我不能else在我的脚本中使用;如何在if else不使用的情况下实现语句else

I'm trying to resize a div:

我正在尝试调整 div 的大小:

function hideTable(){
    var table = document.getElementById('PDemo');
    if(table.style.width == "50%") table.style.width = "150px";
    if(table.style.width == "150px") table.style.width = "50%";
}

This script doesn't work because as soon as the first ifstatement is executed, the condition for the second ifstatement becomes true, so that executes too, thus nothing changes.

该脚本不起作用,因为一旦if执行第一条语句,第二条if语句的条件就变为真,因此它也执行,因此没有任何变化。

P.S. This isn't a homework. The software I am using only allows 1-line statements.

PS 这不是作业。我使用的软件只允许 1 行语句。

回答by Travis Webb

The ternary operator:

三元运算符:

condition ? execTrue() : execFalse();

This is equivalent to:

这相当于:

if (condition) {
    execTrue();
}
else {
    execFalse();
}

You canwrite an if/else in 1 line, just don't press enter...

可以在 1 行中写一个 if/else,只是不要按 Enter...

if (condition) { execTrue(); } else { execFalse(); }

Further, you can write any statement of arbitrary complexity on one line:

此外,您可以在一行中编写任意复杂性的任何语句:

if(condition1) { exec1(); } else if(condition2) { exec2(); } else { execFalse() }

EDIT:I have to ask: wtf kind of horrible software only allows you to write one-line statements? Is this some form of torture? This just sounds absolutely miserable

编辑:我不得不问:wtf 那种可怕的软件只允许你写一行语句?这是某种形式的折磨吗?这听起来绝对悲惨

EDIT 2:If you're having the issue where the second if statement executes as well, you want to impose mutual exclusion, i.e., using else ifinstead of just ifon each subsequent conditional

编辑 2:如果您遇到第二个 if 语句也执行的问题,您想强加互斥,即使用else if而不是仅if在每个后续条件上

-tjw

-tjw

回答by Marcel Hymanwerth

Save the value into a different variable.

将值保存到不同的变量中。

function hideTable(){
  var table = document.getElementById('PDemo');
  var width = table.style.width;
  if(width == "50%") table.style.width = "150px";
  if(width == "150px") table.style.width = "50%";
}

回答by Sean Patrick Floyd

if(/*condition a*/){/*statements a*/}else if(/*condition b*/){/*statements b*/}else{/*statements c/*}

One line only.

只有一行。

回答by alex

If you can't use an else(and that is crazy), use a switch.

如果您不能使用 an else(这很疯狂),请使用switch.

switch (table.style.width) {

    case '50%':
       ...
       break;

    case '150px':
       ...
       break;

}

回答by Joseph Le Brech

with a while loop for the sake of uniqueness.

为了唯一性,有一个while循环。

while (!resized){
  if(width == "50%") {table.style.width = "150px";resized=true;break};
  if(width == "150px") table.style.width = "50%";resized=true;break};
  resized=true;
}

What's the software that you are using?

你用的是什么软件?

回答by Musa

With && operator are called short-circuit operator. It will return the value of the second operand based on the value of the first operand.

与 && 运算符称为短路运算符。它将根据第一个操作数的值返回第二个操作数的值。

For example: (explanation : If 'profile' permission exist in permArray then load profile of user)

例如:(说明:如果 permArray 中存在 'profile' 权限,则加载用户的个人资料)

isPermitted(permArray,'profile') && loadProfile();

The && operator is useful also for checking for null objects before accessing their attributes. For example...

&& 运算符对于在访问它们的属性之前检查空对象也很有用。例如...

var name = person && person.getName();