Copying Objects in JavaScript
Learn about object references and the benefits of using the spread operator.
We'll cover the following...
We have discussed the basics of objects previously, but now we’re going to take a more in-depth look at object-oriented programming and how we can use JavaScript to create classes.
Object references and shared memory
An important concept in JavaScript is that objects are assigned by reference. This means that, if two variables are assigned to the same object, both variables will point to the same object in memory.
For example, imagine we’re creating a weather app and the variable monday
is pointing to a sunny image object. If we assign the variable tuesday
to the same image object using the code const tuesday = monday
, both variables will be pointing to the same image object, as can be seen in the diagram below:
If we then update the image object that tuesday
is pointing to, it will also change the image object that monday
is pointing to, as can be seen in the diagram below, where tuesday
...