Created
July 1, 2019 09:12
-
-
Save MichalBryxi/8f964d1e5730b6ed20a32a906485b895 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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