Nice academic example but has a small wording inaccuracy. ‘Copy’ doesn’t point to ‘items’, it points to the same memory space as ‘items’ points to.

This behavior is the same as any memory efficient language. If you have values that need to be replicate it, you don’t replicate the data such that it takes double the memory it variables that are expected to represent the same values point to the same memory space.

But what comes next is a silly idiosyncrasy of JS. When you change one variable, in other languages you would get a new memory space, depending on how the clone was created and what expectancies are attached to that. If I make a copy expecting that their data are forever similar, the the copy should point to items which points to the data. Therefore regardless how I change items, they will be the same. Otherwise, if I don’t have the expectation and my intention is to be able to manipulate the two independently, then they should both point to the same data but when one is changed, it should already receive a new memory space regardless of the change.

JS plays to one expectation for a give case then subverts it depending on what kind of change you perform.


Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store