JavaScript
Understand what Object.freeze protects
Object.freeze prevents changes to an object’s own properties, but it does not recursively freeze nested objects. Treat it as a shallow integrity constraint rather than a complete immutable-data solution.
Worked example
Node.js example · save as example.mjs and run node example.mjs
const config = Object.freeze({theme: "dark", nested: {size: 10}});
config.nested.size = 20;
console.log(config.nested.size);
console.log(Object.isFrozen(config), Object.isFrozen(config.nested));How it works
The frozen top-level object still holds a reference to an ordinary nested object. Mutating that nested object is allowed because the nested object was not frozen. Freezing is useful for preventing accidental reassignment of a small set of top-level configuration properties.
Limits and mistakes
Assignment to frozen properties can throw in strict mode and fail silently in some non-strict contexts. A recursive freeze helper needs to handle cycles and supported object types carefully. Freezing also does not turn all built-in collection operations into immutable operations.
Check the result
Expect 20, then true false. Check both the outer object and nested objects before assuming a shared configuration cannot change.
Sources
Reference documentation for the behavior used in this guide: