Created
November 3, 2017 00:20
-
-
Save atomize/734975be9c7a79ae55108f768940428c to your computer and use it in GitHub Desktop.
Lookup tables shorthand in javascript.
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
// Longhand | |
if (type === 'aligator') { | |
aligatorBehavior(); | |
} | |
else if (type === 'parrot') { | |
parrotBehavior(); | |
} | |
else if (type === 'dolphin') { | |
dolphinBehavior(); | |
} | |
else if (type === 'bulldog') { | |
bulldogBehavior(); | |
} else { | |
throw new Error('Invalid animal ' + type); | |
} | |
// Shorthand | |
var types = { | |
aligator: aligatorBehavior, | |
parrot: parrotBehavior, | |
dolphin: dolphinBehavior, | |
bulldog: bulldogBehavior | |
}; | |
var func = types[type]; | |
(!func) && throw new Error('Invalid animal ' + type); func(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment