Reflect is a built-in JavaScript object with thirteen static methods for working with objects: reading and writing properties, defining and deleting them, listing keys, checking and changing the prototype, calling functions and creating objects. Each method does something you can already do with an operator or an Object method, but as a plain function with a predictable result.
Many Reflect methods return true or false instead of throwing an error, so you can check whether an action worked. Reflect is also the partner of Proxy: every Proxy trap has a Reflect method with the same name and the same arguments, which makes forwarding the normal behaviour easy.
You never write new Reflect() and you cannot call Reflect() either. It is a plain object, like Math, and you only use its methods, such as Reflect.get(). It works in every modern browser and in Node.js.
Syntax
JavaScript Syntax
Call a Reflect method with the object as the first argument:
JavaScript
const user = { name: "Ann" };
console.log(Reflect.get(user, "name")); // Ann
console.log(Reflect.set(user, "age", 30)); // true
console.log(Reflect.has(user, "age")); // true
console.log(Reflect.ownKeys(user).join(", ")); // name, age
console.log(Reflect.deleteProperty(user, "age")); // true
Reference
Core Ideas
Ideas every Reflect tutorial in this series builds on:
Idea
Example
Remember
Reflect
Reflect.get(obj, "a")
A built-in object that holds the methods. You do not create it
Target
Reflect.has(target, key)
The object you work on. It is always the first argument and must be an object
Property key
"name" or Symbol("id")
The name of a property: a string or a symbol
Boolean result
Reflect.set(obj, "a", 1)
Many methods return true or false instead of throwing
Receiver
Reflect.get(obj, "a", other)
The object used as this inside getters and setters
Prototype
Reflect.getPrototypeOf(obj)
The object that another object inherits from
Proxy
new Proxy(target, handler)
An object that intercepts operations, and often forwards them with Reflect
Compare
Reflect vs Object and Operators
The same job can often be done in two ways. Reflect gives a function with a clear result:
Task
Older way
Reflect way
Check a property
"a" in obj
Reflect.has(obj, "a")
Delete a property
delete obj.a
Reflect.deleteProperty(obj, "a") (returns true or false)
Define a property
Object.defineProperty() (throws on failure)
Reflect.defineProperty() (returns true or false)
Call a function
fn.apply(thisArg, args)
Reflect.apply(fn, thisArg, args)
Create an object
new Fn(...args)
Reflect.construct(Fn, args)
Get all own keys
Object.getOwnPropertyNames() plus symbols
Reflect.ownKeys(obj)
Use the normal syntax in everyday code. Reach for Reflect when you want a boolean result instead of an error, when you work with a Proxy, or when you need to pass the action around as a function. Another difference: most Object methods quietly accept a primitive, while Reflect methods throw a TypeError for anything that is not an object.
Behavior
How Reflect Works
Static methods only
Reflect has no instances and no state. Every method is called on Reflect itself, for example Reflect.get(obj, "a").
The target comes first
The first argument is always the object to work on. If it is not an object (a number, a string, null), the call throws a TypeError.
Failure is a value
Methods such as set(), defineProperty(), deleteProperty() and preventExtensions() return false when the action is not allowed. You decide what to do next, and no try...catch is needed.
Same names as Proxy traps
A Proxy handler can define traps such as get, set and has. Reflect has a method with each of those names, so a trap can call Reflect.get(...arguments) to do the normal thing.
Symbols are keys too
Property keys can be strings or symbols. Methods like ownKeys() include symbol keys and hidden properties, which many older tools skip.
Practice
Real-Life Uses
Reflect is a small toolbox with a few clear jobs. Common uses:
Use
How Reflect helps
Proxy handlers
Forward the normal action after logging, checking or changing something: return Reflect.set(target, key, value, receiver).
Safe property changes
Try to define or delete a property and use the true or false result, with no try...catch.
Calling with an array
Call any function with an array of arguments and a chosen this, even when the function has its own apply property.
Inspecting objects
List every own key, including symbols and hidden ones, and check or change the prototype and extensibility.
Code
JavaScript Examples
Three short programs. Each one prints plain text that is the same every time you run it, and they all run in Node.js.
Example 1 — A Quick Tour of Reflect
Read, set, check and delete a property, list the keys and call a function, all with Reflect methods.
Reflect is a built-in object with static methods for working with objects: get, set, has, delete, define, list keys, change the prototype, call functions and create objects. It is not a constructor, so you never use new with it.
To put the object operations in one place as normal functions with predictable results, and to give every Proxy trap a matching method. It arrived with ES2015, the same version that added Proxy.
Many methods overlap. The main differences are that several Reflect methods return true or false instead of throwing, that Reflect methods throw a TypeError for non-objects instead of converting them, and that Reflect has no constructor or instance methods.
No. Normal syntax such as obj.a, obj.a = 1 and delete obj.a is simpler. Reflect is useful with Proxy, when you want a boolean result instead of an error, or when you need an action as a function.
A Proxy handler has traps such as get, set and has. Reflect has a method with the same name and arguments for each trap, so inside a trap you can call Reflect to do the default behaviour.