Created
January 2, 2020 17:02
-
-
Save fecaps/cbdd7068218b3923964c107a53eb2c8f to your computer and use it in GitHub Desktop.
js examples
This file contains 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
/* ... -> SPREAD OPERATOR */ | |
[ ...[ 1, 3 ], 3, 4 ] | |
/* create array with concat of array and items, result: [ 1, 3, 3, 4 ] */ | |
[ ...[ 1, 2 ], ...[ 3 ] ] | |
/* create array with literal concat of two arrays, result: [ 1, 2, 3 ] */ | |
[ ...[ 1 ] ] | |
/* copy array, result: [ 1 ] */ | |
{ ...{ teste: 'teste' }, ...{ teste1: 'teste1' }} | |
/* create object with assign of two, result: { teste: 'teste', teste1: 'teste1' } | |
it works like: Object.assign({}, { teste: 'teste' }, { teste1: 'teste1' }) */ | |
{ ...{ teste: 'teste' }} | |
/* clone object, result: { teste: 'teste' } */ | |
[ ...'teste' ] | |
/* split string, it works like: 'teste'.split('') | |
result: [ 't', 'e', 's', 't', 'e' ] */ | |
const testFunction = (foo, bar) => { console.log({ foo }); console.log({ bar })} | |
const a = [1, 2] | |
testFunction(...a) | |
/* split all items of array to parameters for a function, result: { foo: 1 } { bar: 2 } */ | |
/*****************************/ | |
/* ... -> REST OPERATOR - Example 1 */ | |
const numbers = [1, 2, 3, 4, 5] | |
[ first, second, ...others ] = numbers | |
first // 1 | |
second // 2 | |
others // [ 3, 4, 5 ] | |
/* ... -> REST OPERATOR - Example 2 */ | |
const { first, second, ...others } = { | |
first: 1, | |
second: 2, | |
third: 3, | |
fourth: 4, | |
fifth: 5 | |
} | |
first // 1 | |
second // 2 | |
others // { third: 3, fourth: 4, fifth: 5 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment