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

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

    <tfoot id='KoF8c'></tfoot>

    <legend id='KoF8c'><style id='KoF8c'><dir id='KoF8c'><q id='KoF8c'></q></dir></style></legend>
      • <bdo id='KoF8c'></bdo><ul id='KoF8c'></ul>

        是否可以在 switch 语句中使用 .contains() ?

        Is it possible to use .contains() in a switch statement?(是否可以在 switch 语句中使用 .contains() ?)

        <small id='5a1o2'></small><noframes id='5a1o2'>

          <bdo id='5a1o2'></bdo><ul id='5a1o2'></ul>
        • <tfoot id='5a1o2'></tfoot>
                <tbody id='5a1o2'></tbody>
              <legend id='5a1o2'><style id='5a1o2'><dir id='5a1o2'><q id='5a1o2'></q></dir></style></legend>
                  <i id='5a1o2'><tr id='5a1o2'><dt id='5a1o2'><q id='5a1o2'><span id='5a1o2'><b id='5a1o2'><form id='5a1o2'><ins id='5a1o2'></ins><ul id='5a1o2'></ul><sub id='5a1o2'></sub></form><legend id='5a1o2'></legend><bdo id='5a1o2'><pre id='5a1o2'><center id='5a1o2'></center></pre></bdo></b><th id='5a1o2'></th></span></q></dt></tr></i><div id='5a1o2'><tfoot id='5a1o2'></tfoot><dl id='5a1o2'><fieldset id='5a1o2'></fieldset></dl></div>
                  本文介绍了是否可以在 switch 语句中使用 .contains() ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这只是我正在尝试做的一个简单示例:

                  This is just a simple example of what I'm trying to do:

                  switch (window.location.href.contains('')) {
                      case "google":
                          searchWithGoogle();
                          break;
                      case "yahoo":
                          searchWithYahoo();
                          break;
                      default:
                          console.log("no search engine found");
                  }
                  

                  如果不可能/不可行,那么更好的选择是什么?

                  If it's not possible/feasible what would be a better alternative?

                  解决方案:

                  在阅读了一些回复后,我发现以下是一个简单的解决方案.

                  After reading some of the responses I found the following to be a simple solution.

                  function winLocation(term) {
                      return window.location.href.contains(term);
                  }
                  switch (true) {
                      case winLocation("google"):
                          searchWithGoogle();
                          break;
                      case winLocation("yahoo"):
                          searchWithYahoo();
                          break;
                      default:
                          console.log("no search engine found");
                  }
                  

                  推荐答案

                  是的",但它不会像你期望的那样.

                  "Yes", but it won't do what you expect.

                  用于切换的表达式被评估一次 - 在这种情况下,contains 评估结果为真/假(例如 switch(true)switch(false)), 不是一个 case 中可以匹配的字符串.

                  The expression used for the switch is evaluated once - in this case contains evaluates to true/false as the result (e.g. switch(true) or switch(false)) , not a string that can be matched in a case.

                  因此,上述方法行不通.除非此模式更大/可扩展,否则只需使用简单的 if/else-if 语句.

                  As such, the above approach won't work. Unless this pattern is much larger/extensible, just use simple if/else-if statements.

                  var loc = ..
                  if (loc.contains("google")) {
                    ..
                  } else if (loc.contains("yahoo")) {
                    ..
                  } else {
                    ..
                  }
                  

                  但是,请考虑是否存在返回google"或yahoo"等的classify 函数,可能使用上述条件.然后它可以这样使用,但在这种情况下可能会过大.

                  However, consider if there was a classify function that returned "google" or "yahoo", etc, perhaps using conditionals as above. Then it could be used as so, but is likely overkill in this case.

                  switch (classify(loc)) {
                     case "google": ..
                     case "yahoo": ..
                     ..
                  }
                  

                  <小时>

                  虽然上面讨论了 JavaScript 中的此类问题,但 Ruby 和 Scala(可能还有其他)提供了处理一些更高级开关"使用的机制.


                  While the above discusses such in JavaScript, Ruby and Scala (and likely others) provide mechanisms to handle some more "advanced switch" usage.

                  这篇关于是否可以在 switch 语句中使用 .contains() ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  如果content取的的事当前页面元素内容时,type类型应该为1 layer.open({type: 1,title: '内容区域',content: $('#DIV_EditUserInfo'), // 设置跳转的div,跳转到对应的页面area: ["920px", "250px"],}); 如果content取的的路径,或者某个页面,type类型应该为
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                  Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)
                      <tfoot id='QgX4L'></tfoot>

                        <bdo id='QgX4L'></bdo><ul id='QgX4L'></ul>
                      • <legend id='QgX4L'><style id='QgX4L'><dir id='QgX4L'><q id='QgX4L'></q></dir></style></legend>
                          <tbody id='QgX4L'></tbody>

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

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