DEV Community

Discussion on: Deep Copy and the Immutability Issue

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.