<bdo id='GRUJx'></bdo><ul id='GRUJx'></ul>

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

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

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

      从 switch 语句中返回是否被认为是比使用 break 更好的做法?

      时间:2023-09-08
            <tbody id='9DhYO'></tbody>

            <bdo id='9DhYO'></bdo><ul id='9DhYO'></ul>
            <legend id='9DhYO'><style id='9DhYO'><dir id='9DhYO'><q id='9DhYO'></q></dir></style></legend>

            <small id='9DhYO'></small><noframes id='9DhYO'>

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

              • 本文介绍了从 switch 语句中返回是否被认为是比使用 break 更好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                选项 1 - 使用 return 切换:

                function myFunction(opt) 
                {
                    switch (opt) 
                    {
                        case 1: return "One";
                        case 2: return "Two";
                        case 3: return "Three";
                
                        default: return "";
                    }    
                }
                

                选项 2 - 使用 break 切换:

                function myFunction(opt) 
                {
                    var retVal = "";
                
                    switch (opt) 
                    {
                        case 1: 
                            retVal = "One";
                            break;
                
                        case 2: 
                            retVal = "Two";
                            break;
                
                        case 3: 
                            retVal = "Three";
                            break;
                    }
                
                    return retVal;
                }
                

                我知道这两种方法都有效,但还有一种最佳做法吗?我倾向于选择选项 1 - 最好使用 return 进行切换,因为它更干净、更简单.

                I know that both work, but is one more of a best practice? I tend to like Option 1 - switch using return best, as it's cleaner and simpler.

                这是我使用@ic3b3rg 评论中提到的技术的具体示例的 jsFiddle:

                var SFAIC = {};
                
                SFAIC.common = 
                {
                    masterPages: 
                    {
                        cs: "CS_",
                        cp: "CP_"
                    },
                
                    contentPages: 
                    {
                        cs: "CSContent_",
                        cp: "CPContent_"    
                    }
                };
                
                function getElementPrefix(page) 
                {
                    return (page in SFAIC.common.masterPages)
                        ? SFAIC.common.masterPages[page]
                        : (page in SFAIC.common.contentPages)
                            ? SFAIC.common.contentPages[page]
                            : undefined;
                }
                

                要调用该函数,我会通过以下方式进行:

                To call the function, I would do so in the following ways:

                getElementPrefix(SFAIC.common.masterPages.cs);
                getElementPrefix(SFAIC.common.masterPages.cp);
                getElementPrefix(SFAIC.common.contentPages.cs);
                getElementPrefix(SFAIC.common.contentPages.cp);
                

                这里的问题是它总是返回未定义的.我猜这是因为它传递的是对象文字的实际值而不是属性.我将如何使用 @ic3b3rg 的 评论中描述的技术来解决此问题?

                Problem here is that it always returns undefined. I'm guessing that it's because it's passing in the actual value of the object literal and not the property. What would I do to fix this using the technique described in @ic3b3rg's comments?

                推荐答案

                中断将允许您继续在函数中处理.如果您只想在函数中执行此操作,只需退出开关即可.

                A break will allow you continue processing in the function. Just returning out of the switch is fine if that's all you want to do in the function.

                这篇关于从 switch 语句中返回是否被认为是比使用 break 更好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:打字稿中开关的替代品 下一篇:如何检测窗口大小然后用 jquery switch 语句做一些事情

                相关文章

                  <bdo id='rCHwU'></bdo><ul id='rCHwU'></ul>
              • <small id='rCHwU'></small><noframes id='rCHwU'>

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

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

                1. <legend id='rCHwU'><style id='rCHwU'><dir id='rCHwU'><q id='rCHwU'></q></dir></style></legend>