Object.assign() (Shallow Copy)

Object.assign() is a method that copies the values of all enumerable properties from one or more source objects to a target object. It creates a shallow copy, meaning that if the properties are objects, their references will be shared between the original and the copied object.

Syntax:

const original = { key: 'value' };
const shallowCopy = Object.assign({}, original);

Example: Here, Object.assign() is used to create a shallow copy (copiedPerson) of the person object. Changes to the top-level properties like firstName in the copied object do not affect the original. However, changes made to nested objects, such as address, are reflected in both the original and the copied object due to shared references.

Javascript




let person = {
    firstName: 'John',
    lastName: 'Doe',
    address: {
        street: 'North 1st street',
        city: 'San Jose',
        state: 'CA',
        country: 'USA'
    }
};
 
let copiedPerson = Object.assign({}, person);
 
copiedPerson.firstName = 'Jane';
 
copiedPerson.address.street = 'Amphitheatre Parkway';
copiedPerson.address.city = 'Mountain View';
 
console.log(copiedPerson);


Output

{
  firstName: 'Jane',
  lastName: 'Doe',
  address: {
    street: 'Amphitheatre Parkway',
    city: 'Mountain View',
    state: 'CA',
    country: 'USA'
  }
}

Copy Constructor in JavaScript

JavaScript does not have a built-in concept of a “copy constructor” like some other programming languages do. However, you can achieve the same result by using techniques for deep and shallow copying.

In JavaScript, when you create an object, it is possible to make copies of that object.

  • Shallow Copy: This method creates a new object, but it only copies the references to the original object’s properties. If the properties are objects themselves, the references to those objects will be shared between the original and the copied object.
  • Deep Copy: This method creates an entirely new and independent copy of the original object and all of its nested objects. Changes made to the properties of the original object or its nested objects do not affect the copied object, and vice versa.

Below are the three different ways to implement a copy constructor in JavaScript, each using a different technique for copying objects:

Table of Content

  • Using Spread Syntax (Shallow Copy)
  • Object.assign() (Shallow Copy)
  • JSON.stringify() and JSON.parse() (Deep Copy)

Similar Reads

Using Spread Syntax (Shallow Copy)

Spread syntax in JavaScript allows you to create a shallow copy of an object by expanding its properties. It creates a new object with copies of the original object’s properties, making it suitable for simple objects and avoiding direct reference sharing....

Object.assign() (Shallow Copy)

...

JSON.stringify() and JSON.parse() (Deep Copy)

Object.assign() is a method that copies the values of all enumerable properties from one or more source objects to a target object. It creates a shallow copy, meaning that if the properties are objects, their references will be shared between the original and the copied object....