I have been cranking out posts on lodash as of late, and have come to make a post on the _.cloneDeep method in lodash which can be used to deep clone objects in javaScript if I am using lodash in a project. However I think it is called for to write a post on a subject that has to do with objects in general with javaScript regardless if lodash is used or not when it comes to the subject of referencing vs copying objects in javaScript.
You see objects in javaScript are not the same thing as primitive values like numbers and strings, that are always copied by value. When it comes to making a copy of a number value, that copy is now a whole other value in memory, and I can change that value without changing the other value from which it is copied. However when it comes to copying objects, often you are not making a copy of the object, you are just creating another reference to the same object. So then this is where cloning of objects comes into play, as well as shallow and deep cloning them, any understanding the difference between what a value is and what a reference to a value is.
So in this post I will be touching base on the topic of copying by value, and copying by reference in javaScript. Also in this post I will be covering a bunch of ways to go about making a copy of an object with native javaScript by itself. In addition I will try to do my best to get the core of the situation with objects in general down when it comes to copying and referencing them.
Read More