Course

Deep Cloning

Loading...

In this lesson, you'll learn about deep cloning in JavaScript, which is essential for creating independent copies of complex objects without maintaining references to the original.

Deep Cloning

Deep cloning involves creating a complete copy of an object, including all nested objects, so that changes to the new object do not affect the original. This is crucial when working with complex data structures.

Cloning an Object with Nested Objects

Consider the following object:

const person = {
    firstName: 'Emma',
    car: {
        brand: 'BMW',
        color: 'blue',
        wheels: 4,
    }
};

1. Using the Spread Operator

We can use the spread operator to create a shallow copy of the object:

const newPerson = { ...person };

This removes the reference from the outer object, allowing us to change properties like without affecting the original:

newPerson.firstName = 'Mia';

console.log(person); // { firstName: 'Emma', car: { brand: 'BMW', color: 'blue', wheels: 4 } }
console.log(newPerson); // { firstName: 'Mia', car: { brand: 'BMW', color: 'blue', wheels: 4 } }

However, if we change a property of the nested object:

newPerson.car.color = 'red';

console.log(person); // { firstName: 'Emma', car: { brand: 'BMW', color: 'red', wheels: 4 } }
console.log(newPerson); // { firstName: 'Mia', car: { brand: 'BMW', color: 'red', wheels: 4 } }

Both objects are affected because the object is still referenced.

2. Creating a Deep Clone

To create a deep clone, we need to remove references from all nested objects. One way to achieve this is by using and .

const newPerson = JSON.parse(JSON.stringify(person));

This method converts the object to a string and then back to an object, effectively removing all references.

Testing the Deep Clone

Let's test the deep clone by modifying the new object:

newPerson.firstName = 'Mia';
newPerson.car.color = 'red';

console.log(person); // { firstName: 'Emma', car: { brand: 'BMW', color: 'blue', wheels: 4 } }
console.log(newPerson); // { firstName: 'Mia', car: { brand: 'BMW', color: 'red', wheels: 4 } }

The original object remains unchanged, confirming that is a deep clone.

Conclusion

Deep cloning is a powerful technique for working with complex objects in JavaScript. By using and , you can create independent copies of objects, ensuring that changes to one do not affect the other. Mastering this concept is crucial for managing data effectively in your applications. If you're still a bit confused, take your time to review this section and practice with different examples. Mastery takes time!

Loading...

0 Comments

"Please login to view comments"

glass-bbok

Join the Conversation!

Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.

Upgrade your account
tick-guideNext Lesson

DOM Introduction