Skip to curriculum
Jump to the editor

5.7. Freeze an object deeply

Medium
Topics
15–30 min

Object.freeze(config) looks like it makes config read-only, but it only freezes the top level: config.server.port = 1 still works, because config.server is a different object that nobody froze. Code that shares state, such as a store's state tree, default settings or constants, often wants the whole tree locked, so that an accidental write fails instead of quietly changing data that other parts of the app rely on.

Write deepFreeze(value) in deep-freeze.js. It freezes value and every object reachable from it through its own properties, and returns value.

Signature

JAVASCRIPT
function deepFreeze(value) // returns value
  • value: any value. Objects, arrays and functions are frozen, with everything inside them; primitives are returned as they are.
  • Returns the same value.

Requirements

  1. value is frozen, and so is every object stored in its properties, at every level: nested objects, arrays and the items in them, and functions (and objects stored on them).
  2. All own properties count: string keys and symbol keys, enumerable and non-enumerable.
  3. Primitives (numbers, strings, booleans, null, undefined, BigInts) are returned unchanged, without an error.
  4. Cycles and shared objects are fine: an object that refers to itself, or one object stored in several places, is frozen once and the call finishes.
  5. An object that is already frozen is still searched: a frozen parent with a mutable child ends with the child frozen.
  6. Getters are never called, so an object a getter returns is not frozen.
  7. Prototypes are not frozen, and neither is anything the object only inherits.
  8. It works for objects without a prototype and for nesting 1,000 levels deep. The data itself does not change.
  9. After deepFreeze, every object in the tree is locked. Assigning to a data property, adding a property by assignment or deleting one is ignored in sloppy code and throws a TypeError in strict-mode code. Changing a property's definition with Object.defineProperty throws a TypeError in both, and Reflect.defineProperty returns false instead. Accessor properties stay accessors: assigning to one still calls its setter, which may change state outside the tree (a variable it closes over). Freezing fixes properties, not the code they run.

Examples

JAVASCRIPT
const config = deepFreeze({ server: { port: 80 }, tags: ["a"] });
config.server.port = 1;   // ignored (TypeError in strict mode)
config.tags.push("b");    // TypeError
config.server.port;       // 80
JAVASCRIPT
const node = { name: "root" };
node.self = node;
deepFreeze(node);         // finishes: the cycle is handled
deepFreeze(42);           // 42
JAVASCRIPT
const child = { count: 1 };
deepFreeze({ parent: Object.freeze({ child }) });
Object.isFrozen(child);   // true

Environment

  • Plain JavaScript in a browser page; the tests call the global deepFreeze from deep-freeze.js.
  • Every built-in is allowed. The contents of a Map or a Set (which are not properties) and typed arrays with items (which Object.freeze refuses) are out of scope.

Hints

  1. How can you look at a property without reading its value, so that a getter does not run?
  2. Object.isFrozen(parent) is true, but is its child frozen? What should decide whether you skip an object?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…