What does Reflect do?
What Reflect is
Reflect is a built-in object (like Math or JSON) that contains static methods for working with objects and functions at a low level.
Put simply:
Reflectis a "unified API" for basic operations on objects: reading, writing, calling, creating, and so on. Its methods correspond to the same operations that can be intercepted throughProxy.
Why it is needed
Before ES6, operations like delete obj.prop or obj[prop] = value were performed via "built-in syntax", but had no functional equivalent.
Reflect was introduced to:
- Make these operations available as functions;
- Simplify writing
Proxytraps (execution can be delegated throughReflect); - Make the API symmetric and safe.
Example 1 - safely reading and writing properties
const user = { name: "Tim" };
console.log(Reflect.get(user, "name")); // "Tim"
Reflect.set(user, "age", 30);
console.log(user.age); // 30The equivalent:
user.name; // get
user.age = 30; // setBut with Reflect you can use this functionally, passing target, key, receiver by hand.
Example 2 - removing a property
const user = { name: "Tim" };
delete user.name; // traditional syntax
// The Reflect equivalent:
Reflect.deleteProperty(user, "name");The difference: Reflect.deleteProperty returns true/false instead of throwing an exception if the property is "configurable: false".
Example 3 - defining a property with flags
const user = {};
Reflect.defineProperty(user, "role", {
value: "admin",
writable: false
});
console.log(user.role); // admin(this is the functional equivalent of Object.defineProperty)
Example 4 - checking whether a property exists
const user = { name: "Tim" };
console.log("name" in user); // true
console.log(Reflect.has(user, "name")); // trueExample 5 - getting the keys
const user = { a: 1, b: 2 };
console.log(Reflect.ownKeys(user)); // ['a', 'b']Unlike Object.keys(), it returns all keys, including Symbol.
Example 6 - controlling the prototype
const user = {};
const proto = { greet() { console.log("hi"); } };
Reflect.setPrototypeOf(user, proto);
Reflect.getPrototypeOf(user).greet(); // "hi"Example 7 - calling a function
function sum(a, b) {
return a + b;
}
console.log(Reflect.apply(sum, null, [2, 3])); // 5This is the safe equivalent of Function.prototype.apply.call(sum, ...)
Example 8 - creating instances
class User {
constructor(name) {
this.name = name;
}
}
const user = Reflect.construct(User, ["Tim"]);
console.log(user.name); // "Tim"The full list of Reflect methods
| Method | What it does |
|---|---|
| Reflect.apply(target, thisArg, args) | Calls a function with the given this and arguments |
| Reflect.construct(target, args) | The equivalent of new target(...args) |
| Reflect.defineProperty(target, key, descriptor) | Creates/changes a property |
| Reflect.deleteProperty(target, key) | Removes a property (the equivalent of delete) |
| Reflect.get(target, key, receiver?) | Returns the property's value |
| Reflect.set(target, key, value, receiver?) | Sets the property's value |
| Reflect.has(target, key) | Checks whether the property exists (in) |
| Reflect.ownKeys(target) | Returns all keys (including Symbol) |
| Reflect.getOwnPropertyDescriptor(target, key) | Returns the property's descriptor |
| Reflect.preventExtensions(target) | Prevents adding new properties |
| Reflect.isExtensible(target) | Checks whether properties can be added |
| Reflect.getPrototypeOf(target) | Gets the prototype |
| Reflect.setPrototypeOf(target, proto) | Sets the prototype |
Example 9 - Reflect + Proxy (the classic pairing)
const user = { name: "Tim", age: 25 };
const proxy = new Proxy(user, {
get(target, prop, receiver) {
console.log(`Reading property: ${prop}`);
return Reflect.get(target, prop, receiver); // delegate safely
},
set(target, prop, value, receiver) {
console.log(`Changing property: ${prop}`);
return Reflect.set(target, prop, value, receiver);
}
});
proxy.name; // Reading property: name
proxy.age = 30; // Changing property: ageUsing Reflect inside traps guarantees the behavior is correct even if the object has getters, setters, or a prototype.
The difference between Reflect and Object
| Feature | Object | Reflect |
|---|---|---|
| Orientation | Methods for working with an object's structure | Universal low-level operations |
| Returning errors | Methods can throw exceptions | Methods more often return false |
| Functionality | Operators cannot be called as functions | They can (for example, Reflect.deleteProperty instead of delete) |
| Connection to Proxy | None | Methods correspond 1-to-1 with Proxy traps |
Summary
| What it does | Example |
|---|---|
| Reads a property | Reflect.get(obj, "x") |
| Writes a property | Reflect.set(obj, "x", 10) |
| Checks for existence | Reflect.has(obj, "x") |
| Removes a property | Reflect.deleteProperty(obj, "x") |
| Gets the keys | Reflect.ownKeys(obj) |
| Calls a function | Reflect.apply(fn, thisArg, args) |
| Creates an object | Reflect.construct(Class, args) |
| Works with the prototype | Reflect.getPrototypeOf(obj) / Reflect.setPrototypeOf(obj, proto) |
The main idea:
Reflectis a "mirror" of JavaScript's basic operations, in the form of functions. It makes code cleaner, safer, and more predictable, especially when working withProxy.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.