Created
January 23, 2017 11:50
-
-
Save b2whats/581b84cb549187bb3447a877af087ad9 to your computer and use it in GitHub Desktop.
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
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