在 JavaScript 中,从 switch 语句返回是否被认为是比使用 break 更好的做法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6114210/
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
In JavaScript, is returning out of a switch statement considered a better practice than using break?
提问by Code Maverick
Option 1 - switch using return:
选项 1 - 使用返回切换:
function myFunction(opt)
{
switch (opt)
{
case 1: return "One";
case 2: return "Two";
case 3: return "Three";
default: return "";
}
}
Option 2 - switch using break:
选项 2 - 使用中断切换:
function myFunction(opt)
{
var retVal = "";
switch (opt)
{
case 1:
retVal = "One";
break;
case 2:
retVal = "Two";
break;
case 3:
retVal = "Three";
break;
}
return retVal;
}
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.
我知道两者都有效,但还有一种最佳实践吗?我倾向于喜欢选项 1 - 最好使用 return 切换,因为它更干净、更简单。
Here is a jsFiddle of my specific example using the technique mentioned in @ic3b3rg's comments:
这是我使用@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);
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'scomments?
这里的问题是它总是返回未定义。我猜这是因为它传递的是对象文字的实际值而不是属性。我将如何使用@ic3b3rg评论中描述的技术来解决此问题?
回答by ic3b3rg
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 中返回就可以了。
回答by Mark Costello
It depends, if your function only consists of the switch statement, then I think that its fine. However, if you want to perform any other operations within that function, its probably not a great idea. You also may have to consider your requirements right now versus in the future. If you want to change your function from option one to option two, more refactoring will be needed.
这取决于,如果你的函数只包含 switch 语句,那么我认为它很好。但是,如果您想在该函数内执行任何其他操作,这可能不是一个好主意。您可能还必须考虑现在与将来的要求。如果要将函数从选项一更改为选项二,则需要进行更多重构。
However, given that within if/else statements it is best practice to do the following:
但是,鉴于在 if/else 语句中,最佳做法是执行以下操作:
var foo = "bar";
if(foo == "bar") {
return 0;
}
else {
return 100;
}
Based on this, the argument could be made that option one is better practice.
基于此,可以认为选项一是更好的做法。
In short, there's no clear answer, so as long as your code adheres to a consistent, readable, maintainable standard - that is to say don't mix and match options one and two throughout your application, that is the best practice you should be following.
简而言之,没有明确的答案,因此只要您的代码遵循一致、可读、可维护的标准——也就是说,不要在整个应用程序中混合和匹配选项一和选项二,这是您应该做的最佳实践下列的。