Last active
July 15, 2016 22:48
-
-
Save hunan-rostomyan/d63cc2769fa81c3a2638aa7026a45a11 to your computer and use it in GitHub Desktop.
Some ways of organizing dictionaries
This file contains 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
// Globals | |
declare const foo: Function; | |
declare const bar: Function; | |
declare const dict: Object; | |
// Option 1 (bad) | |
const functions1 = [ | |
{fn: foo, key: 'FOO'}, | |
{fn: bar, key: 'BAR'} | |
]; | |
functions1.forEach(o => o.fn(dict[o.key])); | |
// Option 2 (better) | |
const functions2 = { | |
FOO: foo, | |
BAR: bar | |
}; | |
// 2.1 (using for..in) | |
for (var fn in functions2) { | |
functions2[fn](dict[fn]); | |
} | |
// 2.2 (using Object.keys) | |
Object.keys(functions2).forEach(key => { | |
functions2[fn](dict[fn]); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment