Last active
October 14, 2016 21:47
-
-
Save iansinnott/819d15fd0d98b114ffd88b9c78e8a64b 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
| const bleh = ['b', 'c', 'd']; | |
| // Want arr === ['a', 'b', 'c', 'd'] | |
| const arr = [ 'a', ...bleh ]; // ['a', 'b', 'c', 'd'] | |
| // Also for objects | |
| const basicInfo = { | |
| name: 'Person', | |
| age: 21, | |
| }; | |
| const person = { | |
| ...someValues, | |
| occupation: 'Belt Wearer', | |
| }; // { name: 'Person', age: 21, occupation: 'Belt Wearer' }; | |
| // Function args can be directly destructured. NOTE that all of these functions below return the same object. Also note that | |
| // I use const more than once for the same var which would be an error in real code. | |
| // Take 1 | |
| const Person = (opts) => { | |
| const age = opts.age; | |
| const name = opts.name; | |
| return { | |
| age, | |
| name, | |
| occupation: 'Freelancer', | |
| }; | |
| }; | |
| // Take 2 | |
| const Person = (opts) => { | |
| const { age, name } = opts; | |
| return { | |
| age, | |
| name, | |
| occupation: 'Freelancer', | |
| }; | |
| }; | |
| // Take 3 | |
| const Person = ({ age, name }) => { | |
| return { | |
| age, | |
| name, | |
| occupation: 'Freelancer', | |
| }; | |
| }; | |
| // Take 3 (another version) | |
| const Person = (opts) => { | |
| return { | |
| ...opts, | |
| occupation: 'Freelancer', | |
| }; | |
| }; | |
| // Take 4 :D | |
| const Person = (opts) => ({ ...opts, occupation: 'Freelancer' }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment