Object.assign()
JavaScript
medium
15 mins
The Object.assign() method copies all enumerable own properties from one or more source objects to a target object. It returns the modified target object. Your task is to replicate this functionality.
- If the target is
nullorundefined, throw aTypeError. - Only enumerable and own properties should be copied.
- Later source properties overwrite earlier ones if they have the same key.
- The method should return the target object.
Example Inputs & Outputs
customAssign({a: 1}, {b: 2}) β {a: 1, b: 2} customAssign({a: 1}, {a: 2, b: 3}) β {a: 2, b: 3} customAssign({}, {a: undefined}, {b: null}) β {a: undefined, b: null}
Constraints & Edge Cases
targetmust not benullorundefined- Only own, enumerable properties of source objects should be copied
- If a source is
nullorundefined, it should be skipped (not throw an error) - Must return the modified
targetobject - Symbol properties should be ignored in this simplified version
Solve Similar questions π₯
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
