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.
Reflect.has({ x: 1 }, "x"); // true (same as "x" in obj) Reflect.ownKeys({ a: 1, [Symbol("s")]: 2 }); // ["a", Symbol(s)]
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
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.
Reflectis a plain object likeMath. - Forward in proxy traps.
Reflect.get(t, k, receiver)inside agettrap keeps getters and inheritance working;target[k]does not pass the receiver. - Booleans instead of exceptions.
Reflect.defineProperty()is the non-throwing counterpart ofObject.defineProperty(); writeif (!Reflect.defineProperty(...))instead oftry/catch. ownKeysis complete, unlikeObject.keys(), which returns only enumerable string keys. Its order is integer-like keys ascending, then other strings in creation order, then symbols.applyis the safe form offn.apply. It works even if the function overrodeapply, and requires an array-like argument list.constructwithnewTargetlets you run one class's constructor while the result inherits from another prototype; this is how class hierarchies are built reflectively.- No
Reflect.getOwnPropertyNamesorReflect.keys. UseownKeysand filter it. - Strict-mode difference.
Reflect.set()on a frozen object returnsfalse; the assignment operator in strict mode throws.
Browser support
Reflect is supported in every current browser and in Node.js.