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

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

      1. <small id='sZcbh'></small><noframes id='sZcbh'>

        <tfoot id='sZcbh'></tfoot>

        为什么 Switch 语句仅适用于 true 关键字?

        Why Switch statement only working with true keyword?(为什么 Switch 语句仅适用于 true 关键字?)

        • <small id='cEAzY'></small><noframes id='cEAzY'>

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

                  本文介绍了为什么 Switch 语句仅适用于 true 关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  谁能向我解释为什么第一个不工作而第二个工作?

                  Can anyone explain to me why first one is not working and second one is working?

                  第一句话

                  function test(n) {
                      switch (n) {
                      case (n == 0 || n == 1):
                          console.log("Number is either 0 or 1");
                          break;
                      case (n >= 2):
                          console.log("Number is greater than 1")
                          break;
                      default:
                          console.log("Default");
                      }
                  }
                  

                  第二个陈述

                  function test(n) {
                      switch (true) {
                      case (n == 0 || n == 1):
                          console.log("Number is either 0 or 1");
                          break;
                      case (n >= 2):
                          console.log("Number is greater than 1")
                          break;
                      default:
                          console.log("Default");
                      }
                  }
                  

                  推荐答案

                  提供给开关的参数将使用===进行比较.如果你有,你有表达式导致 boolean 类型:n==0 ||n==1n >= 2.当您传递一个 number 时,它会尝试将您的 number 与 case 表达式给出的结果进行比较.例如,对于给定的数字 1 它会尝试比较 1 === (1 == 0 || 1 == 1) -> 1 === true 返回 false(严格比较).所以你每次都会得到 Default 文本.

                  The parameter which is given to the switch will be compared using ===. In cases which you have, you have expressions which result to boolean type: n==0 || n==1 or n >= 2. When you pass a number , it tries to compare your number with a result given from the expression in cases. So for example with the given number 1 it tries to compare 1 === (1 == 0 || 1 == 1) -> 1 === true which returns false (strict comparison). So you get the Default text every time.

                  对于第一种情况,您需要在 switch 的 cases 中有数字,而不是 boolean (n==0 || n==1 结果为 boolean).

                  For the first case, you need to have numbers in the cases of your switch , not a boolean (n==0 || n==1 results to boolean).

                  对于第二种情况,你有 boolean 类型的开关值 true.当你再次传递 1 时,比较就像 true === (1 == 0 || 1 == 1) -> true === true 它返回 true.所以你根据你的值 n 得到想要的结果.但是第二种情况没有使用 true 作为值的目标.您可以将其替换为 if else if 语句.

                  With the second case, you have in the switch value true of type boolean.When you pass again 1 the comparing goes like true === (1 == 0 || 1 == 1) -> true === true and it returns true. So you get the desired result according to your value n. But the second case has no goals with using true as the value. You can replace it with a if else if statement.

                  如果您想在多个案例中获得相同的结果,您需要将 2 个案例放在一起.看到这个

                  If you want to get the same result for many cases you need to write 2 cases above each other. See this

                  case 0:
                  case 1:
                    result
                  

                  这里的 case 类型是 number,而不是 boolean.

                  Here the cases have type number, not boolean.

                  代码示例.

                  function test(n){
                      switch (n) {
                      case 0:
                      case 1:
                      console.log("Number is either 0 or 1");
                      break;
                      case 2:
                      console.log("Number is 2")
                      break;
                      default:
                      console.log("Default");}
                  }
                  
                  test(0);
                  test(1);
                  test(2)

                  这篇关于为什么 Switch 语句仅适用于 true 关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
                      <tbody id='qDcKH'></tbody>
                  • <legend id='qDcKH'><style id='qDcKH'><dir id='qDcKH'><q id='qDcKH'></q></dir></style></legend>

                  • <tfoot id='qDcKH'></tfoot>

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

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