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 0
Add a ment  | 

5 Answers 5

Reset to default 8

Just 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