admin管理员组文章数量:1326299
How do I do this in Javascript?
switch(c){
case 'a':
//do something, condition does not match so go to default case
//don't break in here, and don't allow fall through to other cases.
case 'b':
//..
case 'c':
//..
case '_':
//...
default:
//
break;
}
This is a almost a copy of Jumping from one case to the default case in switch statement, but there isn't a solution for javascript, so I have to add this new post.
How do I do this in Javascript?
switch(c){
case 'a':
//do something, condition does not match so go to default case
//don't break in here, and don't allow fall through to other cases.
case 'b':
//..
case 'c':
//..
case '_':
//...
default:
//
break;
}
This is a almost a copy of Jumping from one case to the default case in switch statement, but there isn't a solution for javascript, so I have to add this new post.
Share Improve this question edited May 23, 2017 at 11:51 CommunityBot 11 silver badge asked May 27, 2015 at 0:21 shenkwenshenkwen 3,88010 gold badges54 silver badges101 bronze badges 4- 5 Add the default case logic in a function and call it from both places, is how i would do that. – Shan Robertson Commented May 27, 2015 at 0:30
- 1 Make a method and call it.... – epascarello Commented May 27, 2015 at 0:35
-
Or you could wrap your
switch
inside of a function that takes the switch value as an argument, and if certain condition is met within case'a'
, call the same function again, but with a value that you know will not match any of the cases. Either way, I wouldn't find that to be a great solution. – user4698813 Commented May 27, 2015 at 0:58 -
I don't think a switch statement would be best for what your attempting to solve. Wouldn't it be better to have a nested
if
with multiplemethod
call points? If I had to use this method I would probably do as @ShanRobertson suggested. – lindsay Commented May 27, 2015 at 4:29
3 Answers
Reset to default 2You could always break in the default case and return outside the switch:
switch(c) {
case 'a':
if (something) return 'a'
break
case 'b':
return 'b'
default:
break
}
return 'default stuff'
Another option which might work, depending on your use-case, is to move the default
case out of the switch.
switch(c){
case 'a':
//do something, condition does not match so go to default case
//don't break in here, and don't allow fall through to other cases.
case 'b':
//..
case 'c':
//..
case '_':
//...
}
// Do the `default/finally` here instead
I'm not sure if it's a good idea over nested if statements but this would satisfy your criteria.
It would only enter one case block, then if it satisfies an if statement you can return out of the whole switch statement, otherwise drop down to your default case.
switch(x) {
case 1:
if (something) {
return
}
case 2:
if (somethingElse) {
return
}
default:
return
}
本文标签: javascript go to the default from one case in switch statementStack Overflow
版权声明:本文标题:javascript: go to the default from one case in switch statement - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742206692a2432949.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论