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?
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?
Share Improve this question asked Jul 28, 2014 at 20:17 Ryan LutzRyan Lutz 2452 silver badges12 bronze badges 5- You're looking for function, not a variable. – Matt Ball Commented Jul 28, 2014 at 20:21
- Yes, functions can be set to variables. See this answer: stackoverflow./questions/336859/… – mc01 Commented Jul 28, 2014 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 – Ruan Mendes Commented Jul 28, 2014 at 20:29
- @Matt Ball: Oh ya! I didn't think about that. And it is is answered below brilliantly! – Ryan Lutz Commented Jul 28, 2014 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 mon approaches. – mc01 Commented Jul 28, 2014 at 20:46
4 Answers
Reset to default 12You 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];
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';
}
})();
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 es 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 remend you carefully weigh the novelty of this solution against the future maintenance cost.
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";
};
})();