A deep copy is effectively a clone of an object or array. Unlike a shallow copy, a deep copy is not linked to the original object in any way. All properties and sub-objects are recreated with the same values.
Here’s a simple utility class I created, with a single clone() method that performs a deep copy:
Where would something like this be useful?
Let’s say you have a template for an array of data. When you create a new array, you want to start with the template as a default, but you don’t want to actually reference the template array. So you make a deep copy instead:
Using this method, any changes made to newArray will not affect templateArray.