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 (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 :
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?
case 2
, only the first code is executed, then youbreak
out of the switch statement. The fallthrough does only work forcase 1
. – Carouselswitch
statement is not the way to go. – Throe