Skip to content

Instantly share code, notes, and snippets.

@b2whats
Created January 23, 2017 11:50
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 b2whats/581b84cb549187bb3447a877af087ad9 to your computer and use it in GitHub Desktop.
Save b2whats/581b84cb549187bb3447a877af087ad9 to your computer and use it in GitHub Desktop.
function getSnack (type) {
var snack;
function isDrink () {
return snack = 'Drink';
}
function isFood () {
return snack = 'Food';
}
var snacks = {
'coke': isDrink,
'pepsi': isDrink,
'cookies': isFood,
'crisps': isFood,
};
return snacks[type]();
}
var snack = getSnack('coke');
console.log(snack); // 'Drink'
function getDrink (type) {
var drinks = {
'coke': function () {
return 'Coke';
},
'pepsi': function () {
return 'Pepsi';
},
'lemonade': function () {
return 'Lemonade';
},
'default': function () {
return 'Default item';
}
};
return (drinks[type] || drinks['default'])();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment