Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JS Puzzles · Const

Proxy traps

JavaScript Proxy traps let a handler intercept fundamental operations on a target, such as property access, assignment, deletion, calls, and construction. Traps can customize behavior or delegate transparently to the target.

How it works

A proxy handler can intercept fundamental operations on a target object through named traps. Each trap receives operation-specific arguments and can implement custom behavior or delegate to Reflect.

Syntax

new Proxy(target, handler)

Parameters

NameTypeDescription
targetobject or functionThe original object or function whose operations may be intercepted.
handlerobjectAn object containing trap methods for selected operations.
propertystring or symbolThe property key involved in a property operation.
receiverobjectThe object used as the receiver for get and set operations.

Returns

A Proxy object that forwards unhandled operations to the target and applies the handler's traps where defined.

Key points

  • Create a proxy with a target and a handler; accesses through the proxy can be customized without modifying the target itself.
  • Use get and set to customize reads and assignments, including validation, logging, access control, or computed values.
  • The has trap handles membership checks made with the in operator.
  • The deleteProperty trap intercepts deletion attempts, including deletion of properties that do not exist.
  • A callable proxy can intercept invocation through apply, which receives the target function, this value, and argument list.
  • The construct trap customizes new operations, but the proxy target must itself be constructible.

Example: Intercepting object operations

const target = { name: "Ada", _secret: 42 };
const proxy = new Proxy(target, {
  get(target, property, receiver) {
    if (property === "_secret") throw new Error("private property");
    return Reflect.get(target, property, receiver);
  },
  set(target, property, value, receiver) {
    if (property === "name" && typeof value !== "string") return false;
    return Reflect.set(target, property, value, receiver);
  },
  ownKeys(target) {
    return Reflect.ownKeys(target).filter((key) => !String(key).startsWith("_"));
  }
});

console.log(proxy.name);
proxy.name = "Grace";
console.log(Object.keys(proxy));

The proxy validates assignments, hides a private-looking property, and filters it from key enumeration.

When to use it

Validation

Reject or transform invalid assignments before they reach the target.

Logging and monitoring

Record reads, writes, calls, or other operations for diagnostics.

Access control

Expose a controlled interface while hiding selected properties or operations.

Function and constructor interception

Add behavior to callable or constructible objects without changing their original implementation.

Interview questions

QWhat is a JavaScript Proxy and what does a handler do?

A proxy wraps a target and uses a handler to intercept operations such as property reads, writes, deletion, calls, and construction. The handler can handle an operation itself or forward it to the target, commonly through Reflect.

Follow-up: How can a proxy intercept a function call? Define an apply trap with signature apply(target, thisArg, args), or use a construct trap for new.

QWhich arguments do get and set traps receive?

The get trap runs when a property is read and receives target, property, and receiver. The set trap runs when a property is assigned and receives target, property, value, and receiver; it should return a boolean indicating success.

Follow-up: Why pass receiver to Reflect.get or Reflect.set? It preserves the proxy as the receiver, which matters for inherited accessors and prototype-based behavior.

QWhat restrictions apply to proxy traps?

A proxy cannot freely contradict certain target invariants. For example, a set trap cannot report success while changing a non-writable, non-configurable data property to a different value; violations can produce TypeError.

Follow-up: What other trap can affect enumeration? ownKeys controls keys returned by operations such as Object.keys, while descriptor and configurability rules still constrain the result.

  • Reflect
  • handler.get
  • handler.set
  • handler.has
  • handler.deleteProperty
  • handler.ownKeys
  • handler.apply
  • handler.construct

Source: Proxy and Reflect. Checked 2026-10-15.