Advantages, Disadvantages of Cloning Object with JSON.stringify
TechnoFunnel presents another article on Object Deep Cloning with JSON.stringify in details. We will also discuss about advantages and disadvantages of cloning using JSON.stringify approach. Lets look for the simple implementation of JSON.stringify to clone an Object.
In the above code, we have a simple variable which is first converted to “string” format and saved inside a variable. This variable can then be parsed back to Object form using “JSON.parse”
Once the string has been parsed, the Object is reconstructed out of the stringified Object. Given below is the output:
The Object created above “clonedObject”, is a separate object having a different memory block. The Object is not the reference of the initial object “employeeData”. Lets look into further details about Object cloning using this method.
Advantages of Cloning Object with JSON.stringify
- The Complex Objects are deep cloned using JSON.stringify
- The Object is Deep Cloned using this method.
Lets add another key “address” to the variable “employeeData”. This key is a complex type containing further “key-value” pairs. Given below is the complex Object that we need to clone next.
In the above example we need to ensure that the “address” property is copied by value rather than reference using…