DEV Community

Discussion on: Deep Copy and the Immutability Issue

Collapse
 
habilain profile image
David Griffin

I think you're still off on your understanding of Javascripts data model - that or there's a few errors in what you said. The main thing to realize is that in JavaScript you only have references, and that values/objects are never stored inside a variable. Instead the value/object is stored somewhere in memory and the variable points at that bit of memory.

This is significant for any mutable value/object; for immutable values (e.g. strings and integers) there's no difference (and note that when I say strings and integers are immutable I'm only talking about the values; I can assign a reference to 1 to a variable and then reassign that variable to reference a 2 when I increment the variable. I cannot change the idea of 1 across the entire program into a 2.) If a value is mutable however, then having multiple references to the variable and mutating it causes these issues.

That means there's a few implications for what you've said:

1) You seem to imply that a reference creates a new object; it doesn't. They are the same object (i.e. newObject === initialObject), but you've put a reference to the object into two variables. Because they are the same, a modification on the object pointed at by newObject modifies initialObject.

2) With the Shallow copy, you create a copy of the object and its variables, but those variables still point at mutable objects which are shared with the initial object. Mutating those mutable objects causes the contents of the object to change. But if my shallow copy only contained immutable values, this wouldn't be an issue.

3) A deep copy resolves these issues by making it so that in any case there is a mutable object, the mutable object is copied - and hence variables don't point at the same object twice.

Collapse
 
samdbeckham profile image
Sam Beckham

Those three points were exactly the points I was trying to make. I didn’t go in to how the underlying structures work in JavaScript in order to not get too stuck in the weeds. I did try to point out that variables in JavaScript are references unless you explicitly copy the data though. I’m not sure where the error in what I said was though, can you point me to it so I can update any misleading parts?

Collapse
 
habilain profile image
David Griffin • Edited

I'm not sure if I'm going into how the underlying structures work in any detail - understanding at this level is pretty fundamental to understanding the logic in programming languages like JavaScript which follow this paradigm.

As to an error, it's statements like this: "In this example, newObject is a reference to initialObject. So whenever we get or set data on either of these objects it is also applied to the other object. This is useful in a lot of different ways, but not great for immutability." You've implied that newObject and initialObject are two different objects, when actually they are two references to the same object (and in addition, initialObject isn't an object either; newObject and initialObject just reference the same actual object). There may be a few other places when you've conflated references and objects - I'm currently on a bus and it's hard to type things.

Thread Thread
 
samdbeckham profile image
Sam Beckham

Fair enough. I can see the confusion there. Though It’s a deliberate conflation to make the point easier to understand. It’s not intended to mislead at all.

Thread Thread
 
habilain profile image
David Griffin

I fear that may be a fools economy, because that conflation perpetuates the misunderstanding that led to you writing this post in the first place!

Thread Thread
 
samdbeckham profile image
Sam Beckham

You could be right. I have a feeling there’s another post in here somewhere.