liveBook · Manning

Proxy

As explained in JavaScript in Depth

A JavaScript object that wraps another object and can intercept and customize fundamental operations on it.

A Proxy wraps an object and routes fundamental operations through handler functions called traps. This turns ordinary reads, writes, deletions, and enumeration into interception points for validation, logging, and virtual properties. section 5.8

const originalDrawer = {
  contents: 'tax documents',
  isLocked: false
};

Use the drawer object as the target that the Proxy will wrap. section 5.8

const dynamicDrawer = new Proxy(originalDrawer, {
  get(target, property, receiver) {
    console.log(`Accessing property: ${property}`);
      if (property === 'securityLevel') {
      return target.contents === 'classified'
        ? 'high' : 'normal';
    }
    return target[property];
  },
 
  set(target, property, value, receiver) {
    console.log(`Setting ${property} = ${value}`);
    if (property === 'contents' &&
        typeof value !== 'string') {
      throw new Error('Contents must be a string');
    }
    if (target[property] !== value) {
      console.log(`Changed ${property} from ` +
                  `"${target[property]}" to "${value}"`);
    }
    target[property] = value;
    return true;
  }
});

The handler logs access, computes a virtual securityLevel property, validates contents assignments, and records changes. section 5.8

A normal-looking interaction with intercepted behavior

Reading dynamicDrawer.contents invokes the get trap. Reading securityLevel computes a value that is not stored on the target. Assigning classified invokes the set trap, validates the string, and changes the target; the later securityLevel read reflects the new contents. section 5.8

Use Proxy when ordinary object features are insufficient:

  • Intercept operations that getters and setters cannot cover, including missing-property access and removal. section 5.8
  • Keep interaction syntax ordinary while applying custom validation, logging, or computed-property logic. section 5.8

How JavaScript in Depth covers it

How deep it goes
Explains targets, handlers, traps, and transparent reads and writes through a concrete drawer example.
Examples
Uses a drawer target whose contents are validated and whose computed securityLevel property changes after assignment.
What it assumes
Assumes ordinary objects and getters and setters, then extends them with interception of broader fundamental operations.
What you can do after
Apply via JavaScript's Proxy constructor to intercept property access and assignment with custom handler logic.
How it connects
Connects Proxy to objects, getters and setters, validation, logging, virtual properties, and ECMAScript fundamental operations.

Also explained in

Compare how 6 books explain proxy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.