const something = 2; | |
// Longhand | |
switch (something) { | |
case 1: | |
doSomething(); | |
break; | |
case 2: | |
doSomethingElse(); | |
break; | |
case 3: | |
doSomethingElseAndOver(); | |
break; | |
} | |
// Shorthand | |
var cases = { | |
1: doSomething, | |
2: doSomethingElse, | |
3: doSomethingElseAndOver, | |
}; | |
cases[2](); |
No comments:
Post a Comment