liveBook · Manning

Trap

As explained in Secrets of the JavaScript Ninja, Second Edition

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

A trap is a handler function that runs when a specified interaction passes through a Proxy. Property reads and writes can trigger `get` and `set` traps, letting you customize behavior without adding that logic to each property. section 8.2

Common uses include:

  • Intercepting property reads and writes, such as returning a fallback message when a property is missing. section 8.2
  • Logging access in one reusable Proxy handler instead of mixing logging into domain code or defining it separately for each property. section 8.2.1
Listing 8.7. Creating proxies with the Proxy constructor
const emperor = { name: "Komei" };                           #A
const representative = new Proxy(emperor, {                  #B
  get: (target, key) => {                                    #C
    report("Reading " + key + " through a proxy");           #C
    return key in target ? target[key]                       #C
                         : "Don't bother the emperor!"       #C
  },                                                         #C
  set: (target, key, value) => {                             #C
    report("Writing " + key + " through a proxy");           #C
    target[key] = value;                                     #C
  }                                                          #C
});

assert(emperor.name === "Komei", "The emperor's name is Komei");  #D
assert(representative.name === "Komei",                           #D
      "We can get the name property through a proxy");            #D

assert(emperor.nickname === undefined,                            #E
      "The emperor doesn’t have a nickname ");                    #E
assert(representative.nickname === "Don't bother the emperor!",   #F
      "The proxy jumps in when we make inproper requests");       #F

representative.nickname = "Tenno";                                #G
assert(emperor.nickname === "Tenno",                              #G
      "The emperor now has a nickname");                          #G
assert(representative.nickname === "Tenno",                       #G
      "The nickname is also accessible through the proxy");       #G

See `get` and `set` traps intercept reads and writes through an emperor proxy. section 8.2

Listing 8.9. Using proxies makes it easier to add logging to objects
function makeLoggable(target){                             #A
  return new Proxy(target, {                               #B
    get: (target, property) => {                           #C
      report("Reading " + property);                       #C
      return target[property];                             #C
    },                                                     #C

    set: (target, property, value) => {                       #D
      report("Writing value " + value + " to " + property);   #D
      target[property] = value;                               #D
    }                                                         #D
  });
}

let ninja = { name: "Yoshi"};                              #E
ninja = makeLoggable(ninja);                               #E

assert(ninja.name === "Yoshi", "Our ninja Yoshi");         #F
ninja.weapon = "sword";                                    #F

See a reusable logging handler applied to a ninja object. section 8.2.1

How Secrets of the JavaScript Ninja, Second Edition covers it

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.
How it connects
Links traps to Proxy objects, property access, getters and setters, and logging.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Second Edition. The section links point to the parts of the book it draws on.