Skip to content

Instantly share code, notes, and snippets.

@wking-io
Last active October 11, 2017 02:59
Show Gist options
  • Save wking-io/328cc2276c3a5d5896c0ce008399c2e1 to your computer and use it in GitHub Desktop.
Save wking-io/328cc2276c3a5d5896c0ce008399c2e1 to your computer and use it in GitHub Desktop.
Note to self: Remember to assign the destructured array/object an empty array/object when giving destructured function arguments default values.
const makeSandwich = ({
bread = 'wheat',
meat = 'turkey',
cheese = 'american',
toppings = []
} = {}) => ({
bread,
meat,
cheese,
toppings,
addToppings (...toppings) {
this.toppings = [...this.toppings, ...toppings];
return this;
},
});
const defaultSandwich = makeSandwich();
console.log(defaultSandwich);
/*
{
"bread": "wheat",
"cheese": "american",
"meat": 'turkey',
"toppings": [],
"addToppings": [Function addToppings],
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment