Last active
August 29, 2015 14:07
-
-
Save bigmeech/0309e3c39baa927a11f1 to your computer and use it in GitHub Desktop.
Switch is Evil, Really?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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