Skip to content

Instantly share code, notes, and snippets.

@hunan-rostomyan
Last active July 15, 2016 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hunan-rostomyan/d63cc2769fa81c3a2638aa7026a45a11 to your computer and use it in GitHub Desktop.
Save hunan-rostomyan/d63cc2769fa81c3a2638aa7026a45a11 to your computer and use it in GitHub Desktop.
Some ways of organizing dictionaries
// 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