liveBook · Manning

Trap

A JavaScript Proxy trap is a handler function that intercepts a specific operation performed through the proxy.

How 2 Manning books explain it

Secrets of the JavaScript Ninja, Second Edition

A Proxy handler function that intercepts a particular operation on the target object.

How it is used
Uses traps to intercept object interactions, especially property reads and writes, for access control and logging.
How deep it goes
Explains get and set traps through implicit triggering and examples, rather than detailing a full catalog of traps.
Examples
Demonstrates an emperor proxy with a missing-property response and a ninja proxy that logs reads and writes.
What you can do after
Apply traps through JavaScript’s Proxy constructor to log property access or return a fallback for missing properties.

Read how Secrets of the JavaScript Ninja, Second Edition explains trap →

Secrets of the JavaScript Ninja, Third Edition

A proxy trap is a handler function that intercepts a specific operation performed through a JavaScript proxy.

How it is used
Intercepts object interactions to customize operations such as property reads, assignments, logging, validation, and computed properties.
How deep it goes
Defines trap dispatch, forwarding with Reflect, and several trap types, with a worked example of property access and assignment.
Examples
Demonstrates a proxy that protects an emperor object by customizing reads and rejecting assignments to unknown properties.
What you can do after
Apply proxy traps to intercept property operations, forward them with Reflect, and reject disallowed assignments.

Read how Secrets of the JavaScript Ninja, Third Edition explains trap →

Also covered in

Related concepts

Questions about trap

What is trap?

A JavaScript Proxy trap is a handler function that intercepts a specific operation performed through the proxy.

Which Manning books explain trap?

Secrets of the JavaScript Ninja, Second Edition and Secrets of the JavaScript Ninja, Third Edition.

What kinds of trap are there?

Kinds of trap include get trap and set trap.

What is trap a kind of?

Trap is a kind of function. A function is a callable unit of program code that performs a computation or operation, can accept inputs, and may produce a result or side effect.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.