Update defaults

This commit is contained in:
Isabelle Viktoria Maciohsek
2020-10-19 18:51:24 +03:00
parent 5e8e6f51a3
commit 600ee35644

View File

@ -5,7 +5,9 @@ tags: object,intermediate
Assigns default values for all properties in an object that are `undefined`. Assigns default values for all properties in an object that are `undefined`.
- Use `Object.assign()` to create a new empty object and copy the original one to maintain key order, use `Array.prototype.reverse()` and the spread operator `...` to combine the default values from left to right, finally use `obj` again to overwrite properties that originally had a value. - Use `Object.assign()` to create a new empty object and copy the original one to maintain key order.
- Use `Array.prototype.reverse()` and the spread operator (`...`) to combine the default values from left to right.
- Finally, use `obj` again to overwrite properties that originally had a value.
```js ```js
const defaults = (obj, ...defs) => Object.assign({}, obj, ...defs.reverse(), obj); const defaults = (obj, ...defs) => Object.assign({}, obj, ...defs.reverse(), obj);