JS: Objects
Theory: Cloning (copying)
Object cloning is another common operation in development, especially frontend. Cloning creates a copy of the original object, i.e., a new object filled with the same data.
JavaScript has no built-in function to perform cloning, but you can emulate it using Object.assign(). To do this, just pass an empty object as the first parameter, and the one you want to clone as the second parameter:
The result is two different objects with the same content. Since they are different, changes in one do not change the data in the other.
Cloning is also performed using the clone() function from the lodash library. Although its result is identical to the examples above, it has better operation semantics due to its name.
Cloning with the methods above does not affect nested objects. They end up in the new object from the reference stored in the old one.
This cloning is called shallow cloning. It's very important to remember that this is what JavaScript means when it uses the term "cloning". This is not a bad thing, shallow cloning is handy for many tasks. But if isn't enough, you have to use deep cloning instead.
There is no built-in way in JavaScript to clone objects completely. Therefore, it's customary for developers to use the ready-made cloneDeep() function from the lodash library.
Deep cloning has one serious disadvantage. If the object is large and has a complex structure deep cloning can have a big impact on performance, and this is why it's not a default method.

