JS: Object.assign

By Xah Lee. Date: . Last updated: .

New in ES2015.

Object.assign ( target_obj, source_obj_1, source_obj_2, … )

Shallow copy the values of all of the enumerable own properties from source_obj_1, source_obj_2 etc objects to a target_obj object.

[see JS: Property Attributes, writable, enumerable, configurable]

Return the modified target_obj.

target_obj is modified.

const A = {a1: 3};
const B = {b1: 3};
const T = {};

// add properties from A and B to T. Returns the new T
console.log ( Object.assign ( T, A, B ) ); // { a1: 3, b1: 3 }

// value of variable T is changed
console.log ( T ); // { a1: 3, b1: 3 }
const x1 = {a1: 4};
const x2 = {a1: 3};

// if properties are same, value is overwritten
console.log ( Object.assign ( x2, x1 ) ); // { a1: 4 }

JS Object and Inheritance

  1. Object Overview
  2. Object Type
  3. Find Object's Type
  4. Prototype Chain
  5. Create Object
  6. Object Literal Expr
  7. Create Object with Parent X
  8. Get/Set Parent
  9. Show Prototype Chain
  10. Prevent Adding Property
  11. Clone Object
  12. Test Object Equality
  13. Add Method to Prototype

Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. CSS
  3. JavaScript
  4. JS Obj Ref
  5. DOM
  6. SVG
  7. Blog