admin管理员组文章数量:1277301
I have switch case like as below
switch(something){
case 1:
break;
case 2:
break;
case 3:
break;
default:
break;
}
Now i want to call case 1 within case 3 is it possible?
I have switch case like as below
switch(something){
case 1:
break;
case 2:
break;
case 3:
break;
default:
break;
}
Now i want to call case 1 within case 3 is it possible?
Share Improve this question asked May 29, 2012 at 12:51 JacksonJackson 1,5244 gold badges29 silver badges67 bronze badges 05 Answers
Reset to default 8Just wrap the operations in functions so they are reusable
switch (something) {
case 1:
operation1();
break;
case 2:
operation2();
break;
case 3:
operation3()
operation1(); //call operation 1
break;
default:
defaultOperation();
break;
}
You could do it like so:
switch(something){
case 2:
break;
case 3:
//do something and fall through
case 1:
//do something else
break;
default:
break;
}
normally all code below a matched case will be executed. So if you omit the break;
statement, you will automatically fall through to the next case and execute the code inside.
switch(something){
case 3:
case 1:
break;
case 2:
break;
default:
break;
}
this should do the trick
bine the two cases:
switch(something){
case 1:
case 3:
break;
case 2:
break;
default:
break;
}
var i = 3;
switch (i)
{
case(3):
alert("This is the case for three");
case(1):
alert("The case has dropped through");
break;
case(2):
alert("This hasn't though");
break;
}
Not really a good idea though, leads to unreadable code
本文标签: How to call case within switch case using javascriptStack Overflow
版权声明:本文标题:How to call case within switch case using javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741231169a2362133.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论