Need default settings for a map, or a specialized version of an existing object? Object.assign(target, ...sources) copies values from each source onto the target and returns that same target. It processes sources from left to right, so a later value wins when keys overlap. section 10.1 section 10.3
Fill in map options
const options = { width: 600 }; const mapOptions = Object.assign({ width: 900, height: 500 }, options); // mapOptions is { width: 600, height: 500 } The first object supplies defaults. The supplied width overrides its default; height stays at 500. Here, options itself is unchanged because it is a source, not the target. section 10.1
To extend an object without changing it, start with an empty target: Object.assign({}, base, enhancements). This produces a new outer object; assigning directly into base would change it. Declaring base with const does not prevent that mutation. This technique copies properties—it does not give the result multiple prototypes. section 10.2 section 10.3
Listing 10.4. Copying the spaceship
A reusable spaceship helper copies a shared base into an empty target, then adds each ship’s enhancements without changing the base. section 10.3
Two details matter when the objects are more than simple property bags:
- Object.assign assigns values rather than redefining properties. If the target property has a setter, that setter runs and can reject a value. section 10.4
- Only a source’s own enumerable properties are copied. Copying an array into {} transfers its indexed values, not prototype methods such as reduce; use an array target when those methods are needed. section 10.4

