Javascript 切换案例 - 其他条件

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

Switch case - else condition

javascript

提问by John Cooper

<script type="text/javascript">
//You will receive a different greeting based
//on what day it is. Note that Sunday=0,
//Monday=1, Tuesday=2, etc.

var d = new Date();
var theDay = d.getDay();
switch (theDay)
{
case 5:
  document.write("Finally Friday");
  break;
case 6:
  document.write("Super Saturday");
  break;
case 0:
  document.write("Sleepy Sunday");
  break;
default:
  document.write("I'm looking forward to this weekend!");
}
</script>

If the theDay = 5, then we display Finally Friday. I want if theDay !=5, then display 'Finally Something'.. similarly for others too...

如果theDay = 5,那么我们显示Finally Friday。我想要如果 theDay !=5,然后显示“终于有些东西”.. 对于其他人也是如此......

Is it possible without an If/else condition. If the case 5 does not execute, can i do something else in that place?

没有 If/else 条件是否可能。如果案例 5 没有执行,我可以在那个地方做其他事情吗?

回答by nnnnnn

Is it possible without an If/else condition. If the case 5 does not execute, can i do something else in that place?

没有 If/else 条件是否可能。如果案例 5 没有执行,我可以在那个地方做其他事情吗?

No.

不。

The switch statement will execute the first matching case, and then keep going (ignoring all further case labels) until it gets to either a break statement or the end of the switch block - but even though you can "fall through" to subsequent cases by omitting the break statement the switch does not provide any mechanism to say "do something when this case isn't matched/executed, but also keep trying to look for a matching case".

switch 语句将执行第一个匹配的 case,然后继续执行(忽略所有进一步的 case 标签)直到它到达 break 语句或 switch 块的末尾 - 但即使你可以通过省略 break 语句,switch 不提供任何机制来说明“当这种情况不匹配/执行时做某事,但也要继续尝试寻找匹配的情况”。

What you are describing would normally be done with a series of if/else statements:

您所描述的内容通常是通过一系列 if/else 语句完成的:

var d = new Date(),
    theDay=d.getDay(),
    matched = false;

if (theDay === 5) {
  matched = true;
  document.write("Finally Friday");
} else {
  // your != 5 case here
}
if (theDay === 6) {
  matched = true;
  document.write("Super Saturday");
} else {
  // your != 6 case here
}
if (theDay === 0) {
  matched = true;
  document.write("Sleepy Sunday");
} else {
  // your != 0 case here
}

// default when none matched:
if (!matched) {
  document.write("I'm looking forward to this weekend!");
}

Note that I've added a matchedflag to allow the default to work. And note that there are no else ifstatements because you need every if/else pair to execute.

请注意,我添加了一个matched标志以允许默认值工作。请注意,这里没有else if语句,因为您需要执行每个 if/else 对。

If you are reallydetermined to use a switch statement you could do something sillylike the following:

如果你真的决定使用 switch 语句,你可以做一些像下面这样的傻事

var d = new Date(),
    theDay = d.getDay(),
    c,
    cases = { // pre-list all the "not" cases
             "!5" : true,
             "!6" : true,
             "!0" : true
            };

// add case for theDay and remove the "not" case for theDay (if there is one)
cases[theDay] = true;
if (cases["!" + theDay])
   delete cases["!" + theDay];

for (c in cases) {
   switch(c) {  
      case "5":
         document.write("Finally Friday");
         break;
      case "!5":
         document.write("Finally Something");
         break;
      case "6":
         document.write("Super Saturday");
         break;
      case "!6":
         document.write("Finally Something - but not 6");
         break;
      case "0":
         document.write("Sleepy Sunday");
         break;
      case "!0":
         document.write("Finally Something - but not 0");
         break;
      default:
         document.write("I'm looking forward to this weekend!");
   }
}

If you need the cases to execute in a specific order use an array rather than an object.

如果您需要以特定顺序执行案例,请使用数组而不是对象。

回答by adarshr

You can do something like:

您可以执行以下操作:

var something = 0;

switch (theDay) {
    case 5:
      document.write("Finally Friday");
      something = 15;
      break;
    case 6:
      document.write("Super Saturday");
      something = 16;
      break;
    case 0:
      document.write("Sleepy Sunday");
      something = 20;
      break;
    default:
      document.write("I'm looking forward to this weekend!");
}


switch (something) {
    case 15: document.write("Not Friday");   break;
    case 16: document.write("Not Saturday"); break;
    case 20: document.write("Not Sunday");   break;
    default: document.write("Nothing");      break;
}

回答by James Wiseman

A standard if/elsewould be the best way to achieve this. I wonder why you want to do it without.

标准if/else将是实现这一目标的最佳方式。我想知道你为什么要这样做。

That said, It's not very elegant, but you could try adding the following to the top of your switch statement:

也就是说,它不是很优雅,但您可以尝试将以下内容添加到 switch 语句的顶部:

case 0:
case 1:
case 2:
case 3:
case 4:
case 6:
   document.write("Finally Something");

Giving you:

给你:

switch (theDay)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 6:
   document.write("Finally Something");
case 5:
  document.write("Finally Friday");
  break;
case 6:
  document.write("Super Saturday");
  break;
case 0:
  document.write("Sleepy Sunday");
  break;
default:
  document.write("I'm looking forward to this weekend!");
}

回答by techfoobar

Its like switch does a jump to the matching case, if it doesn't match it will jump to what matches. The answer to your question "If the case 5 does not execute, can i do something else in that place?" is No, because it never reaches that case at all. It jumps to the next matching case or default.

它就像 switch 会跳转到匹配的大小写,如果不匹配,它将跳转到匹配的内容。你的问题的答案“如果案例5没有执行,我可以在那个地方做其他事情吗?” 是不,因为它根本没有达到那种情况。它跳转到下一个匹配的案例或默认值。

回答by Matthew Egan

Overly complicated answers.

过于复杂的答案。

Simplify.

简化。

var switchElse = true;
switch (CHECK_SOMETHING) 
{
    case "SOME_VALUE":
        ...DO SOMETHING...
        switchElse = false;
        break;
    default:
}

if (switchElse) 
{
        ...DO ELSE...
}

The only solution that can be formulated without a compare. USE "DEFAULT" PATTERN

无需比较即可制定的唯一解决方案。 使用“默认”模式

var myValue = "Friday"
switch (CHECK_SOMETHING)
{
    case "SOME_VALUE": 
         myValue = "Some Other Day";
    default:
}