JS: Object.assign

By Xah Lee. Date: . Last updated: .

New in JS2015.

Object.assign(target_obj, source_obj_1, source_obj_2, etc)
Merge objects. Shallow copy all Enumerable own properties from source objects to target object. If property keys clash, later sources overwrites earlier ones.
Return the modified target_obj.
[see Property Attributes]
const jj = { a1: 99 };

// modify a existing object by merging new ones
const aa = { a1: 3 };
const bb = { b1: 3 };
const cc = { c1: 3 };

Object.assign(jj, aa, bb, cc);

console.log(jj.hasOwnProperty("a1"));
console.log(jj.hasOwnProperty("b1"));
console.log(jj.hasOwnProperty("c1"));
console.log(jj.a1 === 3);
// key a1 is overwritten

Shallow copy means, if a source object contains a key whose value is object objX, the result will have objX as a reference, not creating new object, meaning, if objX is modified, it'll show up in both source and result object.

BUY ΣJS JavaScript in Depth