Skip to content

Instantly share code, notes, and snippets.

@MichalBryxi
Created July 1, 2019 09:12
Show Gist options
  • Save MichalBryxi/8f964d1e5730b6ed20a32a906485b895 to your computer and use it in GitHub Desktop.
Save MichalBryxi/8f964d1e5730b6ed20a32a906485b895 to your computer and use it in GitHub Desktop.
// 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
< "๐Ÿ˜น"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment