JS Built-ins
ProxyProxyintermediate

Proxy() β€” get trap returning constant

πŸ“– Usage Example
const target = { value: 42 }
const p = new Proxy(target, {
  get: (t, k) => k in t ? t[k] : 'default'
})
console.log(p.value)   // β†’ 42
console.log(p.missing) // β†’ 'default'

new Proxy(target, handler) creates a proxy that intercepts operations on the target object using trap methods in the handler.

Proxy get trap

When a get trap always returns 42, any property access on the proxy returns 42.

Challenge: Verify that new Proxy({}, { get: () => 42 }).anything === 42.

πŸ’‘ Hints (1)
  • β€’ The `get(target, key)` trap intercepts all property reads.