Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท JSON, Errors and Reflection

The Reflect object

Reference for the JavaScript Reflect object: the 13 static methods that mirror object operations, why they return booleans, and how they pair with Proxy traps.

Reflect is a built-in namespace object (not a constructor) whose static methods perform the fundamental operations of objects: reading and writing properties, defining them, deleting, listing keys, getting and setting the prototype, calling functions and constructing instances. Each method matches one Proxy trap with the same name and arguments.

Many of these operations also exist as operators or Object methods. Reflect gives them a uniform function form, with consistent return values: failures are reported as false instead of an exception where it makes sense, which is easier to compose, and the extra receiver and newTarget arguments are available.

JavaScript
Reflect.has({ x: 1 }, "x");           // true  (same as "x" in obj)
Reflect.ownKeys({ a: 1, [Symbol("s")]: 2 }); // ["a", Symbol(s)]

Try it

JS Operations as functions

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS receiver and newTarget

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
Reflect.get(target, key, receiver?)
Reflect.set(target, key, value, receiver?)
Reflect.has(target, key)
Reflect.deleteProperty(target, key)
Reflect.ownKeys(target)
Reflect.getOwnPropertyDescriptor(target, key)
Reflect.defineProperty(target, key, descriptor)
Reflect.getPrototypeOf(target)
Reflect.setPrototypeOf(target, prototype)
Reflect.isExtensible(target)
Reflect.preventExtensions(target)
Reflect.apply(fn, thisArg, argsList)
Reflect.construct(constructor, argsList, newTarget?)
Method Returns
get The property value. When the property is a getter, receiver is used as this.
set true if the assignment succeeded, otherwise false.
has A boolean, like the in operator.
deleteProperty true if the property was removed or did not exist, false otherwise.
ownKeys An array of all own keys: strings (including non-enumerable) and symbols.
getOwnPropertyDescriptor A descriptor object or undefined.
defineProperty true or false instead of throwing like Object.defineProperty().
getPrototypeOf, setPrototypeOf The prototype, or a boolean success flag.
isExtensible, preventExtensions A boolean.
apply The function's return value.
construct A new instance, whose prototype comes from newTarget.prototype if given.

Exceptions: every method throws a TypeError if target is not an object (unlike some Object.* methods, which coerce primitives). apply throws if the first argument is not callable, construct if it is not a constructor, and exceptions thrown by getters, setters or traps propagate.

Usage notes

  • Not a constructor and not callable. Reflect is a plain object like Math.
  • Forward in proxy traps. Reflect.get(t, k, receiver) inside a get trap keeps getters and inheritance working; target[k] does not pass the receiver.
  • Booleans instead of exceptions. Reflect.defineProperty() is the non-throwing counterpart of Object.defineProperty(); write if (!Reflect.defineProperty(...)) instead of try/catch.
  • ownKeys is complete, unlike Object.keys(), which returns only enumerable string keys. Its order is integer-like keys ascending, then other strings in creation order, then symbols.
  • apply is the safe form of fn.apply. It works even if the function overrode apply, and requires an array-like argument list.
  • construct with newTarget lets you run one class's constructor while the result inherits from another prototype; this is how class hierarchies are built reflectively.
  • No Reflect.getOwnPropertyNames or Reflect.keys. Use ownKeys and filter it.
  • Strict-mode difference. Reflect.set() on a frozen object returns false; the assignment operator in strict mode throws.

Browser support

Reflect is supported in every current browser and in Node.js.