The Reflect namespace object contains static methods for invoking interceptable JavaScript object internal methods. The methods are the same as those of proxy handlers.
Description
Unlike most global objects, Reflect is not a constructor. You cannot use it with the new operator or invoke the Reflect object as a function. All properties and methods of Reflect are static (just like the Math object).
The Reflect object provides a collection of static functions which have the same names as the proxy handler methods.
The major use case of Reflect is to provide default forwarding behavior in Proxy handler traps. A trap is used to intercept an operation on an object — it provides a custom implementation for an object internal method. The Reflect API is used to invoke the corresponding internal method. For example, the code below creates a proxy p with a deleteProperty trap that intercepts the <span class="createlink">Delete</span> internal method. Reflect.deleteProperty() is used to invoke the default <span class="createlink">Delete</span> behavior on targetObject directly. You can replace it with delete, but using Reflect saves you from having to remember the syntax that each internal method corresponds to.
const p = new Proxy(
{},
{
deleteProperty(targetObject, property) {
// Custom functionality: log the deletion
console.log("Deleting property:", property);
// Execute the default introspection behavior
return Reflect.deleteProperty(targetObject, property);
},
},
);
The Reflect methods also allow finer control of how the internal method is invoked. For example, Reflect.construct is the only way to construct a target function with a specific new.target value. If you use the new operator to invoke a function, the new.target value is always the function itself. This has important effects with subclassing. For another example, Reflect.get allows you to run a getter with a custom this value, while property accessors always use the current object as the this value.
Nearly every Reflect method's behavior can be done with some other syntax or method. Some of these methods have corresponding static methods of the same name on Object, although they do have some subtle differences. For the exact differences, see the description for each Reflect method.
Static properties
Reflect[@@toStringTag]- : The initial value of the
@@toStringTagproperty is the string"Reflect". This property is used in Object.prototype.toString.
- : The initial value of the
Static methods
- Reflect.apply
- : Calls a
targetfunction with arguments as specified by theargumentsListparameter. See also Function.prototype.apply.
- : Calls a
- Reflect.construct
- : The
newoperator as a function. Equivalent to callingnew target(...argumentsList). Also provides the option to specify a different prototype.
- : The
- Reflect.defineProperty
- : Similar to Object.defineProperty. Returns a boolean that is
trueif the property was successfully defined.
- : Similar to Object.defineProperty. Returns a boolean that is
- Reflect.deleteProperty
- : The
deleteoperator as a function. Equivalent to callingdelete target[propertyKey].
- : The
- Reflect.get
- : Returns the value of the property. Works like getting a property from an object (
target[propertyKey]) as a function.
- : Returns the value of the property. Works like getting a property from an object (
- Reflect.getOwnPropertyDescriptor
- : Similar to Object.getOwnPropertyDescriptor. Returns a property descriptor of the given property if it exists on the object, undefined otherwise.
- Reflect.getPrototypeOf
- : Same as Object.getPrototypeOf.
- Reflect.has
- : Returns a boolean indicating whether the target has the property. Either as own or inherited. Works like the
inoperator as a function.
- : Returns a boolean indicating whether the target has the property. Either as own or inherited. Works like the
- Reflect.isExtensible
- : Same as Object.isExtensible. Returns a boolean that is
trueif the target is extensible.
- : Same as Object.isExtensible. Returns a boolean that is
- Reflect.ownKeys
- : Returns an array of the target object's own (not inherited) property keys.
- Reflect.preventExtensions
- : Similar to Object.preventExtensions. Returns a boolean that is
trueif the update was successful.
- : Similar to Object.preventExtensions. Returns a boolean that is
- Reflect.set
- : A function that assigns values to properties. Returns a boolean that is
trueif the update was successful.
- : A function that assigns values to properties. Returns a boolean that is
- Reflect.setPrototypeOf
- : A function that sets the prototype of an object. Returns a boolean that is
trueif the update was successful.
- : A function that sets the prototype of an object. Returns a boolean that is
Examples
Detecting whether an object contains certain properties
const duck = {
name: "Maurice",
color: "white",
greeting() {
console.log(`Quaaaack! My name is ${this.name}`);
},
};
Reflect.has(duck, "color");
// true
Reflect.has(duck, "haircut");
// false
Returning the object's own keys
Reflect.ownKeys(duck);
// [ "name", "color", "greeting" ]
Adding a new property to the object
Reflect.set(duck, "eyes", "black");
// returns "true" if successful
// "duck" now contains the property "eyes: 'black'"