最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get variable passed to switch statement - Stack Overflow

programmeradmin2浏览0评论

This isn't the exact use scenario but I was wondering if it was possible to get the value passed to the switch statement without having to retype what is in the switch() part.

Example :

switch(someObject.withSomevalue*(Math.random()*11)) {
    case 1 : alert("one");
    // more cases here
    default: alert(theNumberThatWasPassed);
}

If we run the Math.random() again we'll get another random number that very well could meet one of the cases, so calling what aws called in the switch(x) statement isn't an option. I've been just storing it in a variable - x = someObject.withSomevalue*(Math.random()*11) - and then passing it to the switch that way switch(x), but I was wondering if it's possible to get the value passed to the switch within the switch statement.

This isn't the exact use scenario but I was wondering if it was possible to get the value passed to the switch statement without having to retype what is in the switch() part.

Example :

switch(someObject.withSomevalue*(Math.random()*11)) {
    case 1 : alert("one");
    // more cases here
    default: alert(theNumberThatWasPassed);
}

If we run the Math.random() again we'll get another random number that very well could meet one of the cases, so calling what aws called in the switch(x) statement isn't an option. I've been just storing it in a variable - x = someObject.withSomevalue*(Math.random()*11) - and then passing it to the switch that way switch(x), but I was wondering if it's possible to get the value passed to the switch within the switch statement.

Share Improve this question asked Nov 8, 2010 at 18:01 A Wizard Did ItA Wizard Did It 3,6844 gold badges31 silver badges32 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 5

As everyone else pointed out you have to save it in a variable. But you can do the following in the expression though I do not know how cross browser patible this is:

  switch(x = <your expression>){
    //
    default:alert(x);
  } 

and at least you save one line of code.

This is the same as asking if you can find the values for if(Math.random()){...}. The answer is no, because they are language constructs, and not functions.

Just capture it into a variable before the switch and use that variable.

var myValue = someObject.withSomevalue*(Math.random()*11);
switch(myValue) {
    case 1 : alert("one");
    // more cases here
    default: alert(myValue);
}

I would say your best bet would be to do what you currently are doing, and capture it into the variable before the switch statement. Is there any reason you would not want to do this besides saving a line of code?

Interesting... OP, I'm trying to look at your thought process. Maybe just write out the code how you think it should look like (ignoring that it wouldn't work in the first place).

update:

At least in C/C++, you can just form another block to have the var only be accessible within the switch:


...
{
    var mySwitchVar = blah;
    switch(mySwitchVar) {
        case blah blah blah:
        default blah blah:
    }
}
...

发布评论

评论列表(0)

  1. 暂无评论