JavaScript Proxy & Reflect Interview Questions and Answers

Explore Proxy traps and the Reflect API for intercepting and forwarding object operations.

Frequently Asked Questions

Important questions are highlighted in the sections below; select one to jump to its answer.

Theoretical JavaScript Interview Questions

A Proxy wraps an object and lets you intercept and customize operations on that object, like reading a property, writing a value, or calling a function. You create it with new Proxy(target, handler) where the handler defines the functions (called traps) that intercept operations.

javascript

const obj = { name: "Alice" };

const proxy = new Proxy(obj, {
  get(target, key) {
    console.log("Getting:", key);
    return target[key];
  },
});

console.log(proxy.name); // logs "Getting: name" then "Alice"

Traps are the methods you define in the handler object. Each trap corresponds to a specific operation on the target object.

  • get(target, key) - intercepts property reads
  • set(target, key, value) - intercepts property writes
  • has(target, key) - intercepts the in operator
  • deleteProperty(target, key) - intercepts delete
  • apply(target, thisArg, args) - intercepts function calls
  • construct(target, args) - intercepts new

The Reflect object is a built-in that provides static methods matching the same operations a Proxy can intercept. For example, Reflect.get(target, key) reads a property the same way bracket notation does, and Reflect.set(target, key, value) sets one. Inside a Proxy trap, you typically call the matching Reflect method to carry out the default behavior after running your custom logic.

javascript

const proxy = new Proxy({}, {
  set(target, key, value) {
    console.log("Setting", key, "=", value);
    return Reflect.set(target, key, value); // do the default thing
  }
});

proxy.name = "Alice"; // logs "Setting name = Alice"

You can use the set trap to check the value before it actually gets stored on the object. If the value fails validation, you throw an error. This keeps validation logic in one place.

javascript

const user = new Proxy({}, {
  set(target, key, value) {
    if (key === "age" && (typeof value !== "number" || value < 0)) {
      throw new TypeError("age must be a positive number");
    }
    return Reflect.set(target, key, value);
  }
});

user.age = 25;  // works fine
user.age = -1;  // throws TypeError

Object.defineProperty lets you define a getter/setter for one specific, named property. A Proxy intercepts ALL operations on the entire object, including properties that do not exist yet. This makes Proxy far more flexible for things like reactive data systems and validation. Vue 3 switched from Object.defineProperty (used in Vue 2) to Proxy for exactly this reason.

A Proxy wraps a target and can intercept selected internal operations through handler traps, such as property reads or writes. It can validate or observe behavior, but the proxy is a distinct object from its target.

A `get` trap receives the target, property key, and receiver. The receiver matters for accessors and prototype-based behavior; `Reflect.get()` can forward the operation while preserving it.

A `set` trap returns a Boolean indicating whether the assignment succeeded. Returning false can cause an assignment in strict mode to throw, and a trap must respect Proxy invariants.

`Reflect` exposes methods corresponding to many object internal operations, making it straightforward to forward a trap to the target. It also returns useful success values for operations such as `set` and `defineProperty`.

Proxy invariants are constraints that traps must obey to preserve fundamental object semantics, especially around non-configurable and non-writable properties. Violating them causes the engine to throw a `TypeError`.

The receiver becomes `this` for an accessor getter involved in the lookup, which can differ from the target when the proxy participates in a prototype chain. Forwarding with `Reflect.get(target, key, receiver)` preserves that behavior.

Private fields require the actual branded instance as the receiver; a proxy around that instance does not carry the target's private-field brand. Methods that access private fields may therefore throw when called with the proxy as `this`.

If the target has a non-configurable, non-writable own data property, the `get` trap must return exactly that property's value. Returning a different value violates an invariant and throws a `TypeError`.

After calling the revoker from `Proxy.revocable()`, operations that go through the proxy throw a `TypeError`. The target itself remains usable through any other reference.

JavaScript Practical and Coding Interview Questions

Problem: Create a Proxy around an object with a `count` property. Reject assignments to `count` unless they are non-negative integers; demonstrate a valid assignment and confirm an invalid one leaves the value unchanged.

javascript

const target = { count: 0 };
const counter = new Proxy(target, {
  set(object, property, value) {
    if (property === "count" && (!Number.isInteger(value) || value < 0)) {
      return false;
    }
    object[property] = value;
    return true;
  }
});

counter.count = 4;
console.log(counter.count);
console.log(Reflect.set(counter, "count", -2));
console.log(counter.count);