Advertisement
Guest User

Untitled

a guest
Jul 12th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. // Standard fat-arrow function stored
  2. // as a variable and immediatelly called.
  3. > let foo = () => {return '😹'}; foo()
  4. < "😹"
  5.  
  6. // If there is no block, then JS will
  7. // Implicitly return whatever is the
  8. // result of given statement.
  9. > let foo = () => '😹'; foo()
  10. < "😹"
  11.  
  12. // So what If we want to implicitly
  13. // return a hash?
  14. > let foo = () => {cat: '😹'}; foo()
  15. < undefined
  16. // It does not work, because first lexical
  17. // meaning of curly brackets is a block.
  18. // And 'cat' in this case will be a label.
  19.  
  20. // So to do an implicit return of a hash,
  21. // we have to wrap the hash in parentheses.
  22. > let foo = () ({cat: '😹'}); foo()
  23. < {cat: "😹"}
  24.  
  25. // We can also call the function directly,
  26. // without storing it. But to do that we
  27. // have to wrap the whole function into
  28. // parentheses.
  29. > let foo = (() => ({cat: '😹'})))()
  30. < {cat: "😹"}
  31.  
  32. // And what if now we just want the emoji
  33. // of the cat? We can destructure
  34. // the returned hash.
  35. > let {cat} = (() => ({cat: '😹'}))(); cat
  36. < "😹"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement