Skip to content

Instantly share code, notes, and snippets.

@iansinnott
Last active October 14, 2016 21:47
Show Gist options
  • Select an option

  • Save iansinnott/819d15fd0d98b114ffd88b9c78e8a64b to your computer and use it in GitHub Desktop.

Select an option

Save iansinnott/819d15fd0d98b114ffd88b9c78e8a64b to your computer and use it in GitHub Desktop.
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