JavaScript Reflect

Reflect.get, set, defineProperty, apply, has, and ownKeys, including which methods return a boolean and which throw.

模块页正文目前为英文。

Reflect is a built-in object whose methods correspond to language operations such as property lookup, assignment, and function application. It is often used from Proxy traps so the default behavior stays consistent.

The result type depends on the method. Reflect.set and Reflect.defineProperty return a boolean when the operation is rejected by the target’s existing attributes. Object.defineProperty throws TypeError in the same situation. Reflect.apply and Reflect.get still throw when the input is not valid, for example when apply’s target is not callable, or when a getter descriptor is not a function. Invalid arguments are not turned into false.

Examples were checked in Node.js 22, which implements these ECMAScript built-ins. See the MDN documentation for the complete API.

JavaScript tutorial and API overview →

API index

APISummary
Reflect.get and Reflect.setRead or write a property. set returns false when the write is rejected. get uses receiver as this for accessors.
Reflect.defineProperty and Reflect.applydefineProperty returns false when a legal descriptor is rejected by the target. apply throws TypeError when the target is not callable.

get, set, and receiver

Reflect.get(target, property, receiver) and Reflect.set(target, property, value, receiver) perform ordinary get and set. The optional receiver is used as this when the property is an accessor.

If a data property is non-writable, Reflect.set returns false and leaves the value unchanged. That is a boolean failure, not a thrown error.

Reflect.get and Reflect.set

Reflect.get(target, property, receiver)
Reflect.set(target, property, value, receiver)

Read or write a property. set returns false when the write is rejected. get uses receiver as this for accessors.

ParameterDescription
targetThe object to read or write. It must be an object.
propertyThe property key, a string or symbol.
receiverOptional this value for getters and setters. If omitted, target is used.

Returns: get returns the property value. set returns true if the write succeeded and false if it was rejected.

Throws

  • TypeError — Thrown if target is not an object, or if apply-like calls on Reflect receive a non-callable (see apply below).
javascript
const target = {};
Object.defineProperty(target, "x", { value: 1, writable: false, configurable: false });
console.log(Reflect.get(target, "x"), Reflect.set(target, "x", 2), target.x);
const proto = {
  get val() {
    return this.n;
  },
};
console.log(Reflect.get(proto, "val", { n: 7 }));
console.log(Reflect.has(target, "x"), Reflect.ownKeys(target).join(","));

Expected output

1 false 1
7
true x

Related: Reflect.defineProperty, Proxy, Official Reflect.get

MDN documentation →

defineProperty and apply

Reflect.defineProperty(target, property, descriptor) returns false when the property cannot be redefined, for example when it is non-configurable and the new value conflicts. Object.defineProperty throws TypeError for that same conflict.

A malformed descriptor can still throw. Reflect.apply(target, thisArg, args) throws TypeError when target is not callable; it does not return false. Choose the method’s documented result style rather than assuming every Reflect function is boolean.

Reflect.defineProperty and Reflect.apply

Reflect.defineProperty(target, property, attributes)
Reflect.apply(target, thisArg, argumentsList)

defineProperty returns false when a legal descriptor is rejected by the target. apply throws TypeError when the target is not callable.

ParameterDescription
attributesA property descriptor. An invalid descriptor, such as a non-callable get, throws TypeError rather than returning false.
argumentsListAn array or array-like list of arguments for apply.

Returns: defineProperty returns a boolean. apply returns the return value of the called function.

Throws

  • TypeError — Thrown by apply when target is not callable, and by defineProperty when the descriptor itself is invalid.
javascript
const target = {};
Object.defineProperty(target, "x", { value: 1, writable: false, configurable: false });
console.log(Reflect.defineProperty(target, "x", { value: 2 }));
try {
  Object.defineProperty(target, "x", { value: 2 });
} catch (error) {
  console.log(error.name);
}
try {
  Reflect.apply(undefined, undefined, []);
} catch (error) {
  console.log(error.name);
}

Expected output

false
TypeError
TypeError

Related: Object.assign and hasOwn, Official Reflect.defineProperty

MDN documentation →