Object.assignLoading saved progress…

Object.assign

Object.assign copies the properties of one or more source objects onto a target object and returns that target. It's the classic way to merge objects, apply defaults, or shallow-clone with Object.assign({}, source) — and it copies only what a source owns and exposes.

Implement objectAssign(target, ...sources). For each source (left to right), copy its own enumerable properties — both string-keyed and symbol-keyed — onto target, mutating it. Skip null/undefined sources, let later sources overwrite earlier ones, and return target.

Signature

function objectAssign(target, ...sources) {
  // mutates target with each source's own enumerable properties;
  // returns target.
}

Examples

objectAssign({ a: 1 }, { b: 2 });            // { a: 1, b: 2 }
objectAssign({}, { a: 1 }, { a: 2 });        // { a: 2 }  — later source wins
objectAssign({ a: 1 }, null, { b: 2 });      // { a: 1, b: 2 }  — null skipped
const clone = objectAssign({}, original);    // shallow copy of `original`

Notes

  • Returns the target — and it's the same object, mutated in place, not a copy.
  • Own enumerable only — inherited properties and non-enumerable properties are not copied.
  • Symbols too — own enumerable symbol-keyed properties are copied, not just string keys.
  • Nullish sources are skippednull and undefined sources contribute nothing (no error).
  • Later wins — sources are applied in order, so a later source overwrites an earlier one's key.
  • Shallow — nested objects are copied by reference, not deep-cloned. A null/undefined target throws a TypeError.
Loading editor…