Recursively freeze an object and all nested objects to make them immutable.
Code
Generalconst deepFreeze = o => {
Object.keys(o).forEach(k => {
if (typeof o[k] === 'object' && o[k] !== null) deepFreeze(o[k]);
});
return Object.freeze(o);
};
deepFreeze(obj);
"Object frozen successfully";Parameters
Object to freeze
Browser·fetch() may be limited by CORS
More JavaScript Snippets
Count Object Properties
Count the number of enumerable properties in an object.
Deep Clone Object
Create a deep copy of an object (handles nested objects and arrays).
Deep Equal
Check if two values are deeply equal by recursively comparing all nested properties. Works with objects, arrays, and primitives.
Deep Merge Objects
Deep merge two objects, combining nested properties.
Flatten Object
Flatten a nested object to a single level with dot notation keys.
Get Nested Value
Safely get a nested property value from an object.