javascript switch 语句的最佳实践

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13569924/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:10:27  来源:igfitidea点击:

best practice with javascript switch statement

javascriptswitch-statement

提问by user1854123

I'm currently working on a jquery script using the switch statement and I was wondering what was the best solution when several 'cases' share some properties, let's say I have this pattern:

我目前正在使用 switch 语句处理 jquery 脚本,我想知道当几个“案例”共享一些属性时最好的解决方案是什么,假设我有这种模式:

switch (settings.xxx) {
case 'case1':
    Execute some code
    break;
case 'case2':
    Execute some code
    break;
case 'case3':
    Execute some code
    break;
}

For each case, I have quite a lot of code that's partly repeated because some properties are common to the 3 cases. So my question is, can I do the same with :

对于每种情况,我都有很多部分重复的代码,因为某些属性对于 3 种情况是通用的。所以我的问题是,我可以用以下方法做同样的事情:

switch (settings.xxx) {

case 'case1':
case 'case2':
case 'case3':
    Execute some code
    break;

}

switch (settings.xxx) {

case 'case1':
case 'case2':
    Execute some code
    break;
case 'case2':
case 'case3':
    Execute some code
    break;

}

Or is it a bad practice?

或者这是一种不好的做法?

回答by Adriano Carneiro

No, it's not bad practice. Actually, it is good practice. This way, you would not repeat yourself.

不,这不是坏习惯。实际上,这是一种很好的做法。这样,您就不会重复自己

Also, take a look at the documentation: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/switch

另外,看看文档:https: //developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/switch

回答by Eric Kent

The best practice is to avoid repeating code. That way if you later need to modify the code, you don't need to remember to do it in multiple places.

最佳做法是避免重复代码。这样,如果您以后需要修改代码,就不需要记住在多个地方进行。

In the situation where some of the code only applies to some cases, you can leave out a break, and the execution will fall through to the next section after executing the first section. This only applies when the special case code can be executed before the general case code. Be careful when you do this, and add comments clearly stating that you are doing it intentionally. Otherwise, if the code is later edited, someone else might not realise that it is intentional, and add the break in where it is missing. See below:

在某些代码只适用于某些情况的情况下,您可以省略一个中断,执行完第一个部分后将执行到下一个部分。这仅适用于特殊情况代码可以在一般情况代码之前执行的情况。这样做时要小心,并添加清楚地说明您是故意这样做的注释。否则,如果代码后来被编辑,其他人可能没有意识到它是故意的,并在缺少的地方添加中断。见下文:

switch (settings.xxx) {
case 'case1':
case 'case2':
    Execute some code that applies to cases 1 & 2
    // break left out to allow execution to fall through
case 'case3':
    Execute some code that applies to cases 1 - 3
    break;
case 'case4':
    Execute different code
    break;
}