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.
| Parameter | Description |
|---|---|
target | The object to read or write. It must be an object. |
property | The property key, a string or symbol. |
receiver | Optional 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).
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 xRelated: Reflect.defineProperty, Proxy, Official Reflect.get