Proxy and Reflect
The Proxy and Reflect JavaScript built-ins intercept and define custom behavior for fundamental language operations (such as property lookup, assignment, enumeration, or function invocation).
Overview
The Proxy and Reflect JavaScript built-ins intercept and define custom behavior for fundamental language operations (such as property lookup, assignment, enumeration, or function invocation).
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 49 | 12 | 18 | 10 | 49 | 10 | |
| The Proxy() constructor creates Proxy objects. | 49 | 12 | 18 | 10 | 49 | 10 |
| The handler.apply() method is a trap for the [[Call]] object internal method, which is used by operations such as function calls. | 49 | 12 | 18 | 10 | 49 | 10 |
| The handler.construct() method is a trap for the [[Construct]] object internal method, which is used by operations such as the Operators/new operator. In order for the new operation to be valid on the resulting Proxy object, the target used to initialize the proxy must itself be a valid constructor. | 49 | 12 | 18 | 10 | 49 | 10 |
| The handler.defineProperty() method is a trap for the [[DefineOwnProperty]] object internal method, which is used by operations such as Object.defineProperty(). | 49 | 12 | 18 | 10 | 49 | 10 |
| The handler.deleteProperty() method is a trap for the [[Delete]] object internal method, which is used by operations such as the Operators/delete operator. | 49 | 12 | 18 | 10 | 49 | 10 |
| The handler.get() method is a trap for the [[Get]] object internal method, which is used by operations such as property accessors. | 49 | 12 | 18 | 10 | 49 | 10 |
| The handler.getOwnPropertyDescriptor() method is a trap for the [[GetOwnProperty]] object internal method, which is used by operations such as Object.getOwnPropertyDescriptor(). | 49 | 12 | 18 | 10 | 49 | 10 |
| The handler.getPrototypeOf() method is a trap for the [[GetPrototypeOf]] object internal method, which is used by operations such as Object.getPrototypeOf(). | 49 | 79 | 49 | 10 | 49 | 10 |
| The handler.has() method is a trap for the [[HasProperty]] object internal method, which is used by operations such as the Operators/in operator. | 49 | 12 | 18 | 10 | 49 | 10 |
| The handler.isExtensible() method is a trap for the [[IsExtensible]] object internal method, which is used by operations such as Object.isExtensible(). | 49 | 12 | 31 | 10 | 49 | 10 |
| The handler.ownKeys() method is a trap for the [[OwnPropertyKeys]] object internal method, which is used by operations such as Object.keys(), Reflect.ownKeys(), etc. | 49 | 12 | 18 | 10 | 49 | 10 |
| The handler.preventExtensions() method is a trap for the [[PreventExtensions]] object internal method, which is used by operations such as Object.preventExtensions(). | 49 | 12 | 22 | 10 | 49 | 10 |
| The handler.set() method is a trap for the [[Set]] object internal method, which is used by operations such as using property accessors to set a property's value. | 49 | 12 | 18 | 10 | 49 | 10 |
| The handler.setPrototypeOf() method is a trap for the [[SetPrototypeOf]] object internal method, which is used by operations such as Object.setPrototypeOf(). | 49 | 12 | 49 | 10 | 49 | 10 |
| The Proxy.revocable() static method creates a revocable Proxy object. | 63 | 12 | 34 | 10 | 63 | 10 |
| Other | ||||||
| The Reflect namespace object contains static methods for invoking interceptable JavaScript object internal methods. The methods are the same as those of proxy handlers. | 49 | 12 | 42 | 10 | 49 | 10 |
| Built-in object | ||||||
| The Reflect.apply() static method calls a target function with arguments as specified. | 49 | 12 | 42 | 10 | 49 | 10 |
| The Reflect.construct() static method is like the Operators/new operator, but as a function. It is equivalent to calling new target(...args). It additionally allows to specify a different new.target value. | 49 | 12 | 42 | 10 | 49 | 10 |
| The Reflect.defineProperty() static method is like Object.defineProperty() but returns a Boolean. | 49 | 12 | 42 | 10 | 49 | 10 |
| The Reflect.deleteProperty() static method is like the Operators/delete operator, but as a function. It deletes a property from an object. | 49 | 12 | 42 | 10 | 49 | 10 |
| The Reflect.get() static method is like the property accessor syntax, but as a function. | 49 | 12 | 42 | 10 | 49 | 10 |
| The Reflect.getOwnPropertyDescriptor() static method is like Object.getOwnPropertyDescriptor(). It returns a property descriptor of the given property if it exists on the object, undefined otherwise. | 49 | 12 | 42 | 10 | 49 | 10 |
| The Reflect.getPrototypeOf() static method is like Object.getPrototypeOf(). It returns the prototype of the specified object. | 49 | 12 | 42 | 10 | 49 | 10 |
| The Reflect.has() static method is like the in operator, but as a function. | 49 | 12 | 42 | 10 | 49 | 10 |
| The Reflect.isExtensible() static method is like Object.isExtensible(). It determines if an object is extensible (whether it can have new properties added to it). | 49 | 12 | 42 | 10 | 49 | 10 |
| The Reflect.ownKeys() static method returns an array of the target object's own property keys. | 49 | 12 | 42 | 10 | 49 | 10 |
| The Reflect.preventExtensions() static method is like Object.preventExtensions(). It prevents new properties from ever being added to an object (i.e., prevents future extensions to the object). | 49 | 12 | 42 | 10 | 49 | 10 |
| The Reflect.set() static method is like the property accessor and assignment syntax, but as a function. | 49 | 12 | 42 | 10 | 49 | 10 |
| The Reflect.setPrototypeOf() static method is like Object.setPrototypeOf() but returns a Boolean. It sets the prototype (i.e., the internal [[Prototype]] property) of a specified object. | 49 | 12 | 42 | 10 | 49 | 10 |
- In Firefox 42, the implementation got updated to reflect the final ES2015 specification: The result is now checked if it is an array and if the array elements are either of type string or of type symbol. Enumerating duplicate own property names is not a failure anymore.
Syntax
const handler = {
get(target, prop) {
return prop in target ? target[prop] : `${prop} is undefined`;
},
set(target, prop, value) {
if (typeof value !== 'number') throw TypeError('Only numbers allowed');
target[prop] = value;
return true;
}
};
const obj = new Proxy({}, handler);
obj.x = 42; // OK
obj.missing; // 'missing is undefined' Live demo
Use cases
Using Proxy and Reflect
The Proxy and Reflect JavaScript built-ins intercept and define custom behavior for fundamental language operations (such as property lookup, assignment, enumeration, or function invocation).
Cautions
- No specific concerns. Stable across all major browsers.
Accessibility
- When updating the DOM dynamically, announce important changes to assistive technology with aria-live regions.
Related links
Powered by web-features