liveBook · Manning

Object.assign

As explained in Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond

A JavaScript static method that copies the values of enumerable own properties from one or more source objects to a target object and returns that target object.

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
const baseSpaceShip = { fly: function() { // ... fly function implementation }, shoot: function() { // ... shoot function implementation }, destroy: function() { // ... function for when the ship is destroyed } } function enhancedSpaceShip(enhancements) { return Object.assign({}, baseSpaceShip, enhancements); 1 } function createBomberSpaceShip() { return enhancedSpaceShip({ bomb: function() { // ... make the ship drop a bomb } }); } function createStealthSpaceShip() { return enhancedSpaceShip({ stealth: function() { // ... make the ship invisible } }); }

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

How Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond covers it

How deep it goes
Works through source order, target mutation, setters, and own enumerable properties without descending into specification algorithms.
Examples
Uses pizza-tracking map options, enhanced spaceships, a validating setter, and an array copied into an object.
What it assumes
Assumes familiarity with JavaScript objects and properties while explaining Object.assign’s argument order and copy-first pattern through examples.
What you can do after
Apply it via Object.assign to set defaults, extend objects, and choose whether the original object is mutated.
How it connects
Connects object defaults and copying to mutation, const, property setters, and prototype-chain methods.

Also explained in

Compare how 2 books explain Object.assign →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond. The section links point to the parts of the book it draws on.