A deep copy recursively duplicates every level of a nested structure. Unlike a shallow copy, it shares no nested objects or arrays with the original, protecting immutable data from mutating code. section 7.5 section 7.9
Use deep copying at the boundaries of an immutable safe zone:
- Copy immutable data deeply before passing it to code that may mutate it. section 7.5
- Copy data deeply immediately after receiving it from code that may have mutated it. section 7.5
- Prefer copy-on-write for data under your control; deep copies are more expensive because they duplicate everything. section 7.8 section 7.9
Protecting a shopping cart
Before calling a mutating black_friday_promotion() function, create cart_copy with deepCopy(shopping_cart). After the function returns, deep-copy cart_copy again before treating the result as immutable. This copy-before-and-after pattern prevents the function from changing the original cart or retaining a mutable reference into your code. section 7.4
function deepCopy(thing) {
if(Array.isArray(thing)) {
var copy = [];
for(var i = 0; i < thing.length; i++)
copy.push(deepCopy(thing[i])); #1
return copy;
} else if (thing === null) {
return null;
} else if(typeof thing === "object") {
var copy = {};
var keys = Object.keys(thing);
for(var i = 0; i < keys.length; i++) {
var key = keys[i];
copy[key] = deepCopy(thing[key]); #1
}
return copy;
} else {
return thing; #2
}
}
The teaching implementation handles arrays and objects recursively, returns null unchanged, and leaves JSON-legal primitive values unchanged; it is an outline rather than production-ready JavaScript. section 7.10




