<small id='t2dcN'></small><noframes id='t2dcN'>

    <tfoot id='t2dcN'></tfoot>

    1. <legend id='t2dcN'><style id='t2dcN'><dir id='t2dcN'><q id='t2dcN'></q></dir></style></legend>
      <i id='t2dcN'><tr id='t2dcN'><dt id='t2dcN'><q id='t2dcN'><span id='t2dcN'><b id='t2dcN'><form id='t2dcN'><ins id='t2dcN'></ins><ul id='t2dcN'></ul><sub id='t2dcN'></sub></form><legend id='t2dcN'></legend><bdo id='t2dcN'><pre id='t2dcN'><center id='t2dcN'></center></pre></bdo></b><th id='t2dcN'></th></span></q></dt></tr></i><div id='t2dcN'><tfoot id='t2dcN'></tfoot><dl id='t2dcN'><fieldset id='t2dcN'></fieldset></dl></div>

        • <bdo id='t2dcN'></bdo><ul id='t2dcN'></ul>

        JavaScript 开关奇怪的行为

        时间:2023-09-07

          • <bdo id='g9GAa'></bdo><ul id='g9GAa'></ul>

            <i id='g9GAa'><tr id='g9GAa'><dt id='g9GAa'><q id='g9GAa'><span id='g9GAa'><b id='g9GAa'><form id='g9GAa'><ins id='g9GAa'></ins><ul id='g9GAa'></ul><sub id='g9GAa'></sub></form><legend id='g9GAa'></legend><bdo id='g9GAa'><pre id='g9GAa'><center id='g9GAa'></center></pre></bdo></b><th id='g9GAa'></th></span></q></dt></tr></i><div id='g9GAa'><tfoot id='g9GAa'></tfoot><dl id='g9GAa'><fieldset id='g9GAa'></fieldset></dl></div>
          • <legend id='g9GAa'><style id='g9GAa'><dir id='g9GAa'><q id='g9GAa'></q></dir></style></legend>
          • <tfoot id='g9GAa'></tfoot>

                    <tbody id='g9GAa'></tbody>

                  <small id='g9GAa'></small><noframes id='g9GAa'>

                  本文介绍了JavaScript 开关奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下代码片段.

                  var caseObj = function () {
                  
                  }
                  
                  switch (typeof caseObj) {
                  
                      case "function":
                          console.log("it is function");
                  
                      case "object":
                  
                          console.log("It is object now");
                  }
                  

                  它的输出是

                  it is function.
                  It is object now.
                  

                  typeof caseObj 给出输出 function 但它仍然评估case "object" 也是case.

                  But typeof caseObj gives output function but it still evalutes case "object" case also.

                  这怎么可能?我做错了什么吗?

                  How it is possible? Am I doing wrong anythig?

                  typeof caseObj 是给 function,所以它执行那个 case 但它也执行 object 案例.为什么会出现这种奇怪的行为?

                  typeof caseObj is giving function,So it executing that case but it also executing object case.Why this strange behavior?

                  推荐答案

                  问题不在于typeof,而是你错过了案例中的break语句.这将使 casefunction OR object 并执行这两种情况的块.

                  The problem is not with the typeof, but you've missed the break statement in the case. That'll make the case like function OR object and execute the block of both the cases.

                  您错过了 break;case 的 语句.这就是在下一个case中失败的原因.

                  You missed the break; statement for the cases. This is the reason, of falling out in the next case.

                  break 语句终止当前循环、switch 或标号语句,并将程序控制转移到终止语句之后的语句.

                  The break statement terminates the current loop, switch, or label statement and transfers program control to the statement following the terminated statement.

                  var caseObj = function() {
                  
                  }
                  
                  switch (typeof caseObj) {
                  
                    case "function":
                      document.write("it is function");
                      break;
                  
                    case "object":
                  
                      document.write("It is object now");
                      break;
                  }

                  来自答案中的评论:

                  但是如果没有匹配的case并退出switch,它也会崩溃.但是它也会执行caseobject":语句.为什么?

                  来自 MDN

                  如果找到匹配项,程序将执行相关的语句.如果多个案例与提供的值匹配,则选择第一个匹配的案例,即使案例彼此不相等.

                  If a match is found, the program executes the associated statements. If multiple cases match the provided value, the first case that matches is selected, even if the cases are not equal to each other.

                  与每个 case 标签关联的可选 break 语句确保程序在执行匹配的语句后跳出 switch,并在 switch 后面的语句处继续执行.如果省略了break,程序将继续执行switch语句中的下一条语句.

                  The optional break statement associated with each case label ensures that the program breaks out of switch once the matched statement is executed and continues execution at the statement following switch. If break is omitted, the program continues execution at the next statement in the switch statement.

                  这篇关于JavaScript 开关奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Javascript 中字符串的 Switch-Case 未按预期工作 下一篇:用于比较大于或小于数字的值的 switch 语句

                  相关文章

                  <legend id='aTrh1'><style id='aTrh1'><dir id='aTrh1'><q id='aTrh1'></q></dir></style></legend>

                      <i id='aTrh1'><tr id='aTrh1'><dt id='aTrh1'><q id='aTrh1'><span id='aTrh1'><b id='aTrh1'><form id='aTrh1'><ins id='aTrh1'></ins><ul id='aTrh1'></ul><sub id='aTrh1'></sub></form><legend id='aTrh1'></legend><bdo id='aTrh1'><pre id='aTrh1'><center id='aTrh1'></center></pre></bdo></b><th id='aTrh1'></th></span></q></dt></tr></i><div id='aTrh1'><tfoot id='aTrh1'></tfoot><dl id='aTrh1'><fieldset id='aTrh1'></fieldset></dl></div>

                    1. <tfoot id='aTrh1'></tfoot>

                      • <bdo id='aTrh1'></bdo><ul id='aTrh1'></ul>
                    2. <small id='aTrh1'></small><noframes id='aTrh1'>