Reflect Object Methods
Reflect is a built-in object that holds a set of static methods, one for each basic operation JavaScript can perform on an object, such as reading a property, setting one, checking for one or deleting one. Each method does the same as the matching operator or Object method, but as a plain function call. Its main use is inside a Proxy, where it carries out the default behaviour of an operation that the proxy has intercepted. It also helps where code needs a true or false answer rather than an exception.
Reflect is not a constructor, so it cannot be called or used with new:
try { new Reflect();} catch (err) { console.log(err.message);}console.log(typeof Reflect);This prints:
Reflect is not a constructorobjectThe basic operations
Section titled “The basic operations”| Reflect method | Does the same as |
|---|---|
Reflect.get(obj, key) |
obj[key] |
Reflect.set(obj, key, value) |
obj[key] = value, returning true or false |
Reflect.has(obj, key) |
key in obj |
Reflect.deleteProperty(obj, key) |
delete obj[key] |
Reflect.ownKeys(obj) |
every own key, as an array |
Reflect.defineProperty(obj, key, descriptor) |
Object.defineProperty(), returning true or false |
Reflect.apply(fn, thisArg, args) |
fn.apply(thisArg, args) |
Reflect.construct(Fn, args) |
new Fn(...args) |
Reflect.getPrototypeOf(obj) |
Object.getPrototypeOf(obj) |
const item = { sku: 'TEA-01', price: 3.5 };
console.log(Reflect.get(item, 'price'));console.log(Reflect.set(item, 'price', 3.75), item.price);console.log(Reflect.has(item, 'sku'), Reflect.has(item, 'toString'));console.log(Reflect.deleteProperty(item, 'sku'), item);This prints:
3.5true 3.75true truetrue { price: 3.75 }Like the in operator, Reflect.has() also finds inherited properties, which is why toString is reported as present.
Every own key
Section titled “Every own key”Object.keys() returns only enumerable properties with string names. Reflect.ownKeys() returns all of an object’s own keys, including properties that are not enumerable and properties named by symbols:
const item = { sku: 'TEA-01' };Object.defineProperty(item, 'barcode', { value: '5012345678900', enumerable: false });item[Symbol('audit')] = 'checked';
console.log(Object.keys(item));console.log(Reflect.ownKeys(item));This prints:
[ 'sku' ][ 'sku', 'barcode', Symbol(audit) ]Reporting failure instead of throwing
Section titled “Reporting failure instead of throwing”Object.defineProperty() throws when it cannot define a property, and a failed assignment throws in strict mode. Reflect.defineProperty() and Reflect.set() return false instead, so the result can be tested with an if rather than a try...catch:
const item = Object.freeze({ sku: 'TEA-01', price: 3.5 });
console.log(Reflect.set(item, 'price', 4));console.log(Reflect.defineProperty(item, 'stock', { value: 20 }));
try { Object.defineProperty(item, 'stock', { value: 20 });} catch (err) { console.log(err.name);}This prints:
falsefalseTypeErrorCalling a function with a chosen this
Section titled “Calling a function with a chosen this”Reflect.apply() calls a function with a given this and an array of arguments:
function label(prefix, suffix) { return `${prefix} ${this.sku} ${suffix}`;}
const item = { sku: 'TEA-01' };console.log(Reflect.apply(label, item, ['Item', '(in stock)']));This prints:
Item TEA-01 (in stock)It does the same as label.apply(item, [...]), but it still works if the function’s own apply property has been replaced. Call, Apply, and Bind Methods covers the usual forms.
Inside a Proxy
Section titled “Inside a Proxy”A Proxy wraps an object and lets a handler intercept operations on it. Each Reflect method has the same name and arguments as the handler method it pairs with, so a handler can do its extra work and then pass the operation on unchanged:
const item = { sku: 'TEA-01', price: 3.5 };
const logged = new Proxy(item, { set(target, key, value, receiver) { console.log(`Setting ${String(key)} to ${value}`); return Reflect.set(target, key, value, receiver); },});
logged.price = 3.95;console.log(item.price);This prints:
Setting price to 3.953.95The last argument of Reflect.get() and Reflect.set(), called the receiver, is the object that this should refer to if the property has a getter or setter. Passing on the handler’s receiver keeps getters and setters working correctly through the proxy. MDN’s Reflect reference lists every method.
Steve Griffith covers this topic in his video JavaScript ES6 Reflect Object.