Deep Freeze Object
EasyUtility Functions10 min4 tests
Implement a freezeDeep function that recursively freezes an object and all of its nested objects.
Requirements
- Use
Object.freezeon the object and all nested objects - Handle arrays (freeze the array and its object elements)
- Return the frozen object
- Skip non-object values
Example
const obj = { a: { b: { c: 1 } } };
freezeDeep(obj);
obj.a.b.c = 99; // No effect in strict modeHints (4)
- Use Object.freeze to freeze the top-level object.
- Use Object.values to iterate over all property values.
- Recursively freeze any value that is an object and not already frozen.
- Check Object.isFrozen to avoid infinite loops with circular references.
Topics
- Utility Functions
Asked at
Google · Netflix · Microsoft
