Skip to content

Instantly share code, notes, and snippets.

@casprwang
Created December 18, 2016 00:28
Show Gist options
  • Save casprwang/64747268470df82e6c459da3835ea240 to your computer and use it in GitHub Desktop.
Save casprwang/64747268470df82e6c459da3835ea240 to your computer and use it in GitHub Desktop.
ES6 case
function getDrink(type) {
const drinks = new Map([
['coke', () => 'Coke'],
['pepsi', () => 'Pepsi'],
['lemonade', () => 'Lemonade'],
['default', () => 'Default item'],
]);
const drink = drinks.has(type) ? drinks.get(type)() : drinks.get('default')();
return `The drink I chose was ${drink}`;
}
console.log(getDrink('pepsi'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment