2

I've been googling my little heart out, but I either am asking the wrong question, or it's so obscure it doesn't exists. What I would like to know is can you define a variable with a switch inside it. Things to note about my knowledge of programing and on what level I would understand your answer. I'm a graphic/web designer with only a little experience in js.

Something like this...

var variable = {
    switch (case) {
      case 'case1':
        return "something";
      case 'case2':
        return "something";
      case 'case3':
        return "something";
    };
  };

I know I could just make the switch separate and set the variable per case, but I'm just wondering if this can be done. Also can functions be inside variables? And, how would it be done? Would it be inefficient?

Ryan Lutz
  • 147
  • 12
  • You're looking for function, not a variable. – Matt Ball Jul 28 '14 at 20:21
  • Yes, functions can be set to variables. See this answer: http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname?rq=1 – mc01 Jul 28 '14 at 20:25
  • @mc01 What? I don't think the OP is asking to assign a function to a variable, read it carefully, along with existing answers – Juan Mendes Jul 28 '14 at 20:29
  • @Matt Ball: Oh ya! I didn't think about that. And it is is answered below brilliantly! – Ryan Lutz Jul 28 '14 at 20:30
  • @Juan Mendes, He asked "can functions be inside variables, and how would that be done"? That leads me to assume there's some confusion about how one might declare & store a function in a variable, and what the various syntax options are for doing so. I linked to a question that explains the difference between 2 common approaches. – mc01 Jul 28 '14 at 20:46

4 Answers4

12

You will sometimes see object literals used in place of switch statements. For example, the following hypothetical syntax:

var variable = {
  switch (switchOver) {
    case 'case1':
      return 'something1';
    case 'case2':
      return 'something2';
    case 'case3':
      return 'something2';
  };
};

Can be actually written like this:

var variable = {
  'case1': 'something1',
  'case2': 'something2',
  'case3': 'something3'
}[switchOver];

Which is just a shorthand notation for this:

var cases = {
  'case1': 'something1',
  'case2': 'something2',
  'case3': 'something3'
};

var variable = cases[switchOver];
Ionuț G. Stan
  • 160,359
  • 18
  • 179
  • 193
8

return is for returning from a function, not for giving a variable a value. So the answer to the question is No. You can use a self calling function for something that almost looks like what you had.

var content =  (function(){
   switch (options.type) {
      case 'image':
        return "<img src="+href+"/>";
      case 'iframe':
        return "<iframe src="+href+"/>";
      case 'swf':
        return 'something else';
   }        
})();
Juan Mendes
  • 80,964
  • 26
  • 138
  • 189
  • This is what I'm looking for! Hogan had the same answer. How do i accept both? – Ryan Lutz Jul 28 '14 at 20:23
  • 1
    @RyanLutz: You can't choose both. Hohan and Juan will have to fight it out in a battle to the death. That, or you can wait a few minutes and see who has the clearest answer after a barrage of soon-to-come edits. – Casey Falk Jul 28 '14 at 20:24
  • I would saw Juan had the clearest answer, as a variable cannot "contain" a function, can derive its value from a self calling function. At least, that's what I think is being said. – Ryan Lutz Jul 28 '14 at 20:28
  • @RyanLutz I think both answers properly answer the question with a (very) slight edge to mine because I do have a function in there and the question does mention function :p I would accept whichever one you end up using – Juan Mendes Jul 28 '14 at 20:30
  • Hey mine has the function too! @RyanLutz - Give it Juan -- he beat me by 15 seconds. – Hogan Jul 28 '14 at 20:31
  • @Hogan Oops, I thought the OP was choosing between mine and ionuț-g-stan's – Juan Mendes Jul 28 '14 at 20:33
2

Keep it simple. The easiest solution would be something like this:

var content;
switch (options.type) {
  case 'image':
    content = "<img src="+href+"/>"; 
    break;
  case 'iframe':
    content = "<iframe src="+href+"/>"; 
    break;
  case 'swf':
    content = "<object width="+width+" height="+height+" data="+href+"></object>";
}

This is clean, efficient, and easy to understand.

If you really wanted to use a function, you'd probably want to use an immediately invoked function expression (or IIFE):

var content = (function() {
    switch (options.type) {
      case 'image':
        return "<img src="+href+"/>";
      case 'iframe':
        return "<iframe src="+href+"/>";
      case 'swf':
        return "<object width="+width+" height="+height+" data="+href+"></object>";
    })();

But this is (slightly) slower, and harder to understand for the person that comes along after you to maintain your code. Still, it could be useful in some situations, like if you have a bunch of temporary variables that you don't want 'leaking' into the rest of your scope.

I'd recommend you carefully weigh the novelty of this solution against the future maintenance cost.

p.s.w.g
  • 136,020
  • 27
  • 262
  • 299
  • 2
    "I know I could just make the switch separate and set the variable per case, but I'm just wondering if this can be done." I think OP is aware of this solution already. :/ – Casey Falk Jul 28 '14 at 20:20
  • 2
    Usually the advantage of IIFE is that you can localize variables. I wouldn't try to nitpick the OP's example since they are clearly just trying to figure out if something can be done. I don't think this deserves a downvote though – Juan Mendes Jul 28 '14 at 20:24
  • @JuanMendes I'm really not trying to nitpick. I'm trying to offer justifications for choosing one method over the other. – p.s.w.g Jul 28 '14 at 20:26
  • ah didn't see them at the end of the lines. – scrappedcola Jul 28 '14 at 20:27
1

You could assign it an executed function like this:

var variable = (function () {
    switch (case) {
      case 'case1':
        return "something";
      case 'case2':
        return "something";
      case 'case3':
        return "something";
    };
  })();
Hogan
  • 63,843
  • 10
  • 75
  • 106