JavaScript Internals (521 Qs)•Part 4: Prototypes, Object Methods & Event Loop (174 - 253)
What are the applications of the assign method
Below are the some of main applications of Object.assign() method,
- It is used for cloning an object.
- It is used to merge objects with the same properties.
Practical Code Example
javascript
// Applications of Object.assign():
// 1. Merging objects:
const defaults = { theme: 'light', fontSize: 14 };
const userPrefs = { theme: 'dark' };
const merged = Object.assign({}, defaults, userPrefs);
console.log(merged); // { theme: 'dark', fontSize: 14 }
// 2. Cloning an object (shallow copy):
const clone = Object.assign({}, merged);