Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Standard fat-arrow function stored
- // as a variable and immediatelly called.
- > let foo = () => {return '😹'}; foo()
- < "😹"
- // If there is no block, then JS will
- // Implicitly return whatever is the
- // result of given statement.
- > let foo = () => '😹'; foo()
- < "😹"
- // So what If we want to implicitly
- // return a hash?
- > let foo = () => {cat: '😹'}; foo()
- < undefined
- // It does not work, because first lexical
- // meaning of curly brackets is a block.
- // And 'cat' in this case will be a label.
- // So to do an implicit return of a hash,
- // we have to wrap the hash in parentheses.
- > let foo = () ({cat: '😹'}); foo()
- < {cat: "😹"}
- // We can also call the function directly,
- // without storing it. But to do that we
- // have to wrap the whole function into
- // parentheses.
- > let foo = (() => ({cat: '😹'})))()
- < {cat: "😹"}
- // And what if now we just want the emoji
- // of the cat? We can destructure
- // the returned hash.
- > let {cat} = (() => ({cat: '😹'}))(); cat
- < "😹"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement