Guest User

Untitled

a guest
Nov 13th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. ```
  2. // Define the function that returns a direct invocation on the object literal. So cool!
  3. const getDrink = (type) => (
  4. {
  5. 'coke': 'Coke',
  6. 'pepsi': 'Pepsi',
  7. 'lemonade': 'Lemonade'
  8. }[type] // This is the direct invocation.
  9. )
  10.  
  11. getDrink('coke') // "Coke"
  12. ```
  13. ---
  14.  
  15. 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.
  16.  
  17. ```
  18. const getObject = () => ({
  19. 'coke': 'Coke',
  20. 'pepsi': 'Pepsi',
  21. 'lemonade': 'Lemonade'
  22. })
  23.  
  24. const getDrink = (type) => (
  25. getObject()[type]
  26. )
  27.  
  28. getDrink('coke') // "Coke"
  29. ```
  30. ---
  31.  
  32. But can we make this a one liner so that it's a little cleaner?
  33.  
  34. ```
  35. const getDrink = (type) => ({
  36. 'coke': 'Coke',
  37. 'pepsi': 'Pepsi',
  38. 'lemonade': 'Lemonade'
  39. })[type]
  40.  
  41. getDrink('coke') // "Coke"
  42. ```
  43.  
  44. Dunno if this is cleaner but it sure is a one liner.
Add Comment
Please, Sign In to add comment