Last active
October 11, 2017 02:59
-
-
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.
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
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