Creating a Proxy
new Proxy(target, handler) wraps an object. The handler is a plain object whose methods called *traps* intercept operations on the target. If a trap is missing, the operation passes through unchanged.
const target = { name: "Alice", age: 30 }
const handler = {
get(target, prop) {
console.log(`Getting: ${prop}`)
return Reflect.get(target, prop) // forward to target
}
}
const proxy = new Proxy(target, handler)
proxy.name // logs "Getting: name", returns "Alice"
proxy.age // logs "Getting: age", returns 30