Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Reflect

BaselineWidely available

TheReflect namespace object contains static methods for invoking interceptable JavaScript object internal methods. The methods are the same as those ofproxy handlers.

Description

Unlike most global objects,Reflect is not a constructor. You cannot use it with thenew operator or invoke theReflect object as a function. All properties and methods ofReflect are static (just like theMath object).

TheReflect object provides a collection of static functions which have the same names as theproxy handler methods.

The major use case ofReflect is to provide default forwarding behavior inProxy handler traps. Atrap is used to intercept an operation on an object — it provides a custom implementation for anobject internal method. TheReflect API is used to invoke the corresponding internal method. For example, the code below creates a proxyp with adeleteProperty trap that intercepts the[[Delete]] internal method.Reflect.deleteProperty() is used to invoke the default[[Delete]] behavior ontargetObject directly. You can replace it withdelete, but usingReflect saves you from having to remember the syntax that each internal method corresponds to.

js
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);    },  },);

TheReflect 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 specificnew.target value. If you use thenew operator to invoke a function, thenew.target value is always the function itself. This has important effects withsubclassing. For another example,Reflect.get() allows you to run agetter with a customthis value, whileproperty accessors always use the current object as thethis value.

Nearly everyReflect method's behavior can be done with some other syntax or method. Some of these methods have corresponding static methods of the same name onObject, although they do have some subtle differences. For the exact differences, see the description for eachReflect method.

Static properties

Reflect[Symbol.toStringTag]

The initial value of the[Symbol.toStringTag] property is the string"Reflect". This property is used inObject.prototype.toString().

Static methods

Reflect.apply()

Calls atarget function with arguments as specified by theargumentsList parameter. See alsoFunction.prototype.apply().

Reflect.construct()

Thenew operator as a function. Equivalent to callingnew target(...argumentsList). Also provides the option to specify a different prototype.

Reflect.defineProperty()

Similar toObject.defineProperty(). Returns a boolean that istrue if the property was successfully defined.

Reflect.deleteProperty()

Thedelete operator as a function. Equivalent to callingdelete target[propertyKey].

Reflect.get()

Returns the value of the property. Works like getting a property from an object (target[propertyKey]) as a function.

Reflect.getOwnPropertyDescriptor()

Similar toObject.getOwnPropertyDescriptor(). Returns a property descriptor of the given property if it exists on the object,undefined otherwise.

Reflect.getPrototypeOf()

Same asObject.getPrototypeOf().

Reflect.has()

Returns a boolean indicating whether the target has the property. Either as own or inherited. Works like thein operator as a function.

Reflect.isExtensible()

Same asObject.isExtensible(). Returns a boolean that istrue if the target is extensible.

Reflect.ownKeys()

Returns an array of the target object's own (not inherited) property keys.

Reflect.preventExtensions()

Similar toObject.preventExtensions(). Returns a boolean that istrue if the update was successful.

Reflect.set()

A function that assigns values to properties. Returns a boolean that istrue if the update was successful.

Reflect.setPrototypeOf()

A function that sets the prototype of an object. Returns a boolean that istrue if the update was successful.

Examples

Detecting whether an object contains certain properties

js
const duck = {  name: "Maurice",  color: "white",  greeting() {    console.log(`Quaaaack! My name is ${this.name}`);  },};Reflect.has(duck, "color");// trueReflect.has(duck, "haircut");// false

Returning the object's own keys

js
Reflect.ownKeys(duck);// [ "name", "color", "greeting" ]

Adding a new property to the object

js
Reflect.set(duck, "eyes", "black");// returns "true" if successful// "duck" now contains the property "eyes: 'black'"

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-reflect-object

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp