The spread operator (...) is the modern, idiomatic way to merge objects
with defaults in JavaScript (ES2018+). The Object.assign() approach,
while still valid, is more verbose and less readable.
Changes:
- Replace Object.assign() in the Good example with object spread syntax
({...defaults, ...config}) which is more concise and widely used today
- Change 'let' to 'const' since finalConfig is not reassigned
- Fix missing semicolon after 'return finalConfig'
- Update the result comment from 'config now equals' to 'finalConfig equals'
to accurately reflect which variable the comment describes
- Add a backward-compatibility note mentioning Object.assign() as an
equivalent alternative for environments without ES2018 spread support
Closes ryanmcdermott#705
Summary
Updates the "Set default objects with Object.assign" section to showcase the modern object spread operator as the preferred approach, with a backward-compatibility note for older environments.
Motivation
The spread operator ({...defaults, ...config}) has been standard JavaScript (ES2018) for several years and is now supported in all modern browsers and Node.js versions. It is:
Keeping Object.assign() as the primary example in 2024 misleads developers into using a more verbose, less idiomatic API.
Changes
Backward Compatibility Note Added
Closes #705