Object.assign and Shallow Copying
Object composition is a powerful technique in JavaScript that lets you build new objects by combining properties from existing ones. This approach is useful when you want to create objects that share behavior or configuration without relying on inheritance. As your codebase grows, you will often need to copy or merge objects—perhaps to add configuration options, extend defaults, or combine user input with system settings. JavaScript provides several ways to do this, but one of the most common and flexible tools is Object.assign.
12345const defaults = { theme: "light", showSidebar: true }; const userSettings = { theme: "dark" }; const finalSettings = Object.assign({}, defaults, userSettings); console.log(JSON.stringify(finalSettings)); // { theme: "dark", showSidebar: true }
In the code above, Object.assign creates a new object by copying properties from defaults and userSettings into an empty object ({}). If a property exists in more than one source, the last value wins—so in this case, the theme property from userSettings overwrites the one from defaults. This is a common pattern for merging configuration objects.
However, it's important to understand that Object.assign performs a shallow copy. This means it copies property values directly, but if a property value is itself an object or array, only the reference to that object or array is copied—not the object or array itself. Changes to nested objects in one place can affect other objects that share those references. Always be cautious when working with nested data structures to avoid unexpected side effects when using shallow copying.
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Awesome!
Completion rate improved to 7.69
Object.assign and Shallow Copying
Pyyhkäise näyttääksesi valikon
Object composition is a powerful technique in JavaScript that lets you build new objects by combining properties from existing ones. This approach is useful when you want to create objects that share behavior or configuration without relying on inheritance. As your codebase grows, you will often need to copy or merge objects—perhaps to add configuration options, extend defaults, or combine user input with system settings. JavaScript provides several ways to do this, but one of the most common and flexible tools is Object.assign.
12345const defaults = { theme: "light", showSidebar: true }; const userSettings = { theme: "dark" }; const finalSettings = Object.assign({}, defaults, userSettings); console.log(JSON.stringify(finalSettings)); // { theme: "dark", showSidebar: true }
In the code above, Object.assign creates a new object by copying properties from defaults and userSettings into an empty object ({}). If a property exists in more than one source, the last value wins—so in this case, the theme property from userSettings overwrites the one from defaults. This is a common pattern for merging configuration objects.
However, it's important to understand that Object.assign performs a shallow copy. This means it copies property values directly, but if a property value is itself an object or array, only the reference to that object or array is copied—not the object or array itself. Changes to nested objects in one place can affect other objects that share those references. Always be cautious when working with nested data structures to avoid unexpected side effects when using shallow copying.
Kiitos palautteestasi!