--- title: Meta programming slug: Web/JavaScript/Guide/Meta_programming page-type: guide sidebar: jssidebar --- The {{jsxref("Proxy")}} and {{jsxref("Reflect")}} objects allow you to intercept and define custom behavior for fundamental language operations (e.g., property lookup, assignment, enumeration, function invocation, etc.). With the help of these two objects you are able to program at the meta level of JavaScript. ## Proxies {{jsxref("Proxy")}} objects allow you to intercept certain operations and to implement custom behaviors. For example, getting a property on an object: ```js const handler = { get(target, name) { return name in target ? target[name] : 42; }, }; const p = new Proxy({}, handler); p.a = 1; console.log(p.a, p.b); // 1, 42 ``` The `Proxy` object defines a `target` (an empty object here) and a `handler` object, in which a `get` _trap_ is implemented. Here, an object that is proxied will not return `undefined` when getting undefined properties, but will instead return the number `42`. Additional examples are available on the {{jsxref("Proxy")}} reference page. ### Terminology The following terms are used when talking about the functionality of proxies. - {{jsxref("Proxy/Proxy", "handler", "", 1)}} - : Placeholder object which contains traps. - traps - : The methods that provide property access. (This is analogous to the concept of _traps_ in operating systems.) - target - : Object which the proxy virtualizes. It is often used as storage backend for the proxy. Invariants (semantics that remain unchanged) regarding object non-extensibility or non-configurable properties are verified against the target. - {{Glossary("invariant", "invariants")}} - : Semantics that remain unchanged when implementing custom operations are called _invariants_. If you violate the invariants of a handler, a {{jsxref("TypeError")}} will be thrown. ## Handlers and traps The following table summarizes the available traps available to `Proxy` objects. See the [reference pages](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy) for detailed explanations and examples.
| Handler / trap | Interceptions |
|---|---|
| {{jsxref("Proxy/Proxy/getPrototypeOf", "handler.getPrototypeOf()")}} |
{{jsxref("Object.getPrototypeOf()")}} {{jsxref("Reflect.getPrototypeOf()")}} {{jsxref("Object/proto", "__proto__")}} {{jsxref("Object.prototype.isPrototypeOf()")}} {{jsxref("Operators/instanceof", "instanceof")}} |
| {{jsxref("Proxy/Proxy/setPrototypeOf", "handler.setPrototypeOf()")}} |
{{jsxref("Object.setPrototypeOf()")}} {{jsxref("Reflect.setPrototypeOf()")}} |
| {{jsxref("Proxy/Proxy/isExtensible", "handler.isExtensible()")}} |
{{jsxref("Object.isExtensible()")}} {{jsxref("Reflect.isExtensible()")}} |
| {{jsxref("Proxy/Proxy/preventExtensions", "handler.preventExtensions()")}} |
{{jsxref("Object.preventExtensions()")}} {{jsxref("Reflect.preventExtensions()")}} |
| {{jsxref("Proxy/Proxy/getOwnPropertyDescriptor", "handler.getOwnPropertyDescriptor()")}} |
{{jsxref("Object.getOwnPropertyDescriptor()")}} {{jsxref("Reflect.getOwnPropertyDescriptor()")}} |
| {{jsxref("Proxy/Proxy/defineProperty", "handler.defineProperty()")}} |
{{jsxref("Object.defineProperty()")}} {{jsxref("Reflect.defineProperty()")}} |
| {{jsxref("Proxy/Proxy/has", "handler.has()")}} |
|
| {{jsxref("Proxy/Proxy/get", "handler.get()")}} |
|
| {{jsxref("Proxy/Proxy/set", "handler.set()")}} |
|
| {{jsxref("Proxy/Proxy/deleteProperty", "handler.deleteProperty()")}} |
|
| {{jsxref("Proxy/Proxy/ownKeys", "handler.ownKeys()")}} |
{{jsxref("Object.getOwnPropertyNames()")}} {{jsxref("Object.getOwnPropertySymbols()")}} {{jsxref("Object.keys()")}} {{jsxref("Reflect.ownKeys()")}} |
| {{jsxref("Proxy/Proxy/apply", "handler.apply()")}} |
proxy(..args){{jsxref("Function.prototype.apply()")}} and {{jsxref("Function.prototype.call()")}} {{jsxref("Reflect.apply()")}} |
| {{jsxref("Proxy/Proxy/construct", "handler.construct()")}} |
new proxy(...args){{jsxref("Reflect.construct()")}} |