Sub topic 15
Object Spread vs Object.assign
This chapter assumes you already know object spread ({ ...obj }) and why its copies are shallow. Both of those were covered in earlier chapters. Object.assign(target, ...sources) is the older way to copy and merge objects, and you will still find it in many codebases. The two look interchangeable, but they differ in what they change, how they write properties, and what they can take as input.
What They Have in Common
Both copy a source object's own enumerable properties, including symbol-keyed ones. Both read getters on the source and copy the value the getter returns, not the getter function. Both skip sources that are null or undefined. Both apply sources left to right, so later properties win. Both make shallow copies.
const a = { x: 1, nested: { n: 1 } };
const b = { x: 2, y: 3 };
const viaSpread = { ...a, ...b, ...null };
const viaAssign = Object.assign({}, a, b, null);
console.log(viaSpread); // { x: 2, nested: { n: 1 }, y: 3 }
console.log(viaAssign); // { x: 2, nested: { n: 1 }, y: 3 }
console.log(viaSpread.nested === a.nested); // true (shallow)
console.log(viaAssign.nested === a.nested); // true (shallow)Difference 1: Mutation of the Target
Object.assign writes into its first argument and returns that same object. Spread always builds a new object. The most common bug comes from passing a shared object as the target:
const defaults = { retries: 3, timeout: 1000 };
const config = Object.assign(defaults, { timeout: 5000 });
console.log(config === defaults); // true
console.log(defaults.timeout); // 5000 (defaults were changed)To avoid this, pass a fresh {} as the target, or use spread.
Difference 2: Setters Run vs Properties Are Defined
Object.assign uses ordinary assignment (target[key] = value), so it triggers any setters on the target. Spread defines new properties on the new object it builds, so no setter runs.
const target = {
set level(v) {
console.log('setter called with', v);
}
};
Object.assign(target, { level: 1 }); // logs: setter called with 1
const copy = { ...target, level: 2 }; // logs nothing
console.log(copy.level); // 2 (a plain data property)This is why Object.assign is the right tool when you want to update an existing object, such as a class instance with validating setters, through its normal interface.
Difference 3: Prototype and Variable Sources
Spread always produces a plain object whose prototype is Object.prototype. With Object.assign, you choose the target, so you can keep a prototype:
class Point {
constructor(x, y) { this.x = x; this.y = y; }
sum() { return this.x + this.y; }
}
const p = new Point(1, 2);
const spreadCopy = { ...p };
const assignCopy = Object.assign(Object.create(Point.prototype), p);
console.log(typeof spreadCopy.sum); // 'undefined'
console.log(assignCopy.sum()); // 3When the sources come as an array of unknown length, you can merge them with Object.assign({}, ...list). Object literal syntax has no direct equivalent.
Quick Comparison
| Aspect | Object spread | Object.assign |
|---|---|---|
| Result | Always a new object | The target, mutated |
| Setters on target | Not called | Called |
| Prototype of result | `Object.prototype` | Whatever the target has |
| Getters on sources | Read | Read |
| Depth | Shallow | Shallow |
| Array of sources | Needs `reduce` | `Object.assign({}, ...list)` |
Exercise
After running const base = { a: 1 }; const r1 = { ...base, b: 2 }; const r2 = Object.assign(base, { c: 3 });, what are base, r1 === base, and r2 === base?
Show answer
base is { a: 1, c: 3 }, because Object.assign mutated it. r1 === base is false, because spread created a new object (r1 is { a: 1, b: 2 }). r2 === base is true, because Object.assign returns its target.
Exercise
An object has a setter set name(v) that trims whitespace. Which call stores a trimmed name on that same object: obj = { ...obj, name: ' Ada ' } or Object.assign(obj, { name: ' Ada ' })? Why?
Show answer
Object.assign(obj, { name: ' Ada ' }) does, because it assigns through the setter. The spread version builds a new plain object and defines name as a data property without calling the setter, so the whitespace stays and the original object is replaced, not updated.