Deep Cloning Objects
Deep Cloning Objects
Why should one even be interested in this one may ask? Because it will most definitely affect you in many aspects. This is one of the most annoying properties of JS.
myArr is constant!! wtf!!
I am sure you will find a real world application that concerns two arrays like in this example. Before we discuss a solution, let's first talk about why this is.
When you pass in numbers to be copied:
And even strings (remember, in JS, strings are not array pointers like in C and C++):
it copies by value. This is very much what is expected. In the backend, what happens is that the value is passed, and everyone is happy! However, objects and arrays in the backend are just pointers to memory addresses, and you are being given the pointer! Not what you want at all!
Naively, to combat this, one would probably do something like this:
And all is good! However, is it though?
That's right, it only made a shallow copy. Only the first things got copied, nothing else!
To implement a deep copy, all we need to do is deep copy objects within objects. It's quite simple, and here's my implementation of it:
If the argument isn't an object (which we will see later), we just return the object as it will be passed by value, and if it is an object, we just create a new object that gets assigned key by key with copies of the value of the key.
Now, our example looks like this:
it copied! However, the output isn't really an array anymore...
For arrays, we just need to
map the values, and the function looks something like this:
A very nice recursive solution like the object copy.
Everything is passed by value except for objects. Those are stored as pointers, and will be passed by reference. In order to remedy this, we defined two functions.
And special thanks to @fuzzyastrocat, here's a function that accomplishes both:
Now, with these functions at your disposal, go forth and complete the aoc!
Nice, however I'd put a word of caution in this. By definition, cloning an object is expensive performance-wise (and memory-wise for that matter). So, if you're needing to use this a lot then there's probably a better, more efficient way to solve the problem.
However, this is definitely needed sometimes, so I'm not saying it should never be done.
(Is that a typo on line 5 of the copyObj function? Shouldn't it be
Also, here's a single function that can do both objects and arrays: