Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```
- // Define the function that returns a direct invocation on the object literal. So cool!
- const getDrink = (type) => (
- {
- 'coke': 'Coke',
- 'pepsi': 'Pepsi',
- 'lemonade': 'Lemonade'
- }[type] // This is the direct invocation.
- )
- getDrink('coke') // "Coke"
- ```
- ---
- The only thing I don't like is the look of it. I like a lot of syntax sugar in my code :). So let's give it a quick little change to make it a little cleaner.
- ```
- const getObject = () => ({
- 'coke': 'Coke',
- 'pepsi': 'Pepsi',
- 'lemonade': 'Lemonade'
- })
- const getDrink = (type) => (
- getObject()[type]
- )
- getDrink('coke') // "Coke"
- ```
- ---
- But can we make this a one liner so that it's a little cleaner?
- ```
- const getDrink = (type) => ({
- 'coke': 'Coke',
- 'pepsi': 'Pepsi',
- 'lemonade': 'Lemonade'
- })[type]
- getDrink('coke') // "Coke"
- ```
- Dunno if this is cleaner but it sure is a one liner.
Add Comment
Please, Sign In to add comment