Skip to content

Instantly share code, notes, and snippets.

@bigmeech
Last active August 29, 2015 14:07
Show Gist options
  • Save bigmeech/0309e3c39baa927a11f1 to your computer and use it in GitHub Desktop.
Save bigmeech/0309e3c39baa927a11f1 to your computer and use it in GitHub Desktop.
Switch is Evil, Really?
var action1 = function(){
return "Action 1 executed"
};
var action2 = function(){
return "Action 2 executed"
};
var def = function(){
return "Default Action Executed"
};
var aCase = "case1";
//Using a lookup Hash
var cases = {
case1:action1,
case2:action2,
"default":def
};
if(!aCase in cases) aCase = "default";
cases[aCase]("args to pass");
//Using switch
switch(aCase){
case "case1":
action1(args);
break;
case "case2":
action2(args);
break;
default:
def(args);
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment