Deep Clone
Copy a nested structure so nothing is shared — including the cases that break a naive recursion.
JavaScript · ES6 — Pratik Rai ·
Freeze an object all the way down, because Object.freeze only handles the top level.
Write deepFreeze(obj) that freezes an object and everything nested inside it, then returns the same object.
After freezing, writing to a property at any depth must not change it. A structure that contains itself must not recurse forever.
Input:
JSfile.javascript1const config = deepFreeze({ api: { retries: 3 } }); 2config.api.retries = 99; 3config.api.retries;
Output:
3
Object.freeze alone would have left the nested object writable.
Object.isFrozen becomes your cycle guard for free.Goal: Freeze every level and return the same object.
Continue learning with these related challenges
Copy a nested structure so nothing is shared — including the cases that break a naive recursion.
JavaScript · ES6 — Pratik Rai ·
Compare two values structurally, because === only ever compares references.
JavaScript · ES6 — Pratik Rai ·
Group an array into buckets by a key function — now a real language feature as Object.groupBy.
JavaScript · ES6 — Pratik Rai ·
Copy a nested structure so nothing is shared — including the cases that break a naive recursion.
JavaScript · ES6
Pratik Rai ·
Compare two values structurally, because === only ever compares references.
JavaScript · ES6
Pratik Rai ·
Group an array into buckets by a key function — now a real language feature as Object.groupBy.
JavaScript · ES6
Pratik Rai ·