Suggest an editImprove this articleRefine the answer for “What does Reflect do?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`Reflect`** is a built-in object (like `Math` or `JSON`) that contains static methods for working with objects and functions at a low level. **Key point:** it is a unified API for basic object operations (read, write, call, create), whose methods correspond to the same operations that can be intercepted through `Proxy`.Shown above the full answer for quick recall.Answer (EN)Image## 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: > `Reflect` is 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 through `Proxy`. ## 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: 1. Make these operations available as **functions**; 2. Simplify writing `Proxy` traps (execution can be delegated through `Reflect`); 3. Make the API symmetric and safe. ## Example 1 - safely reading and writing properties ```javascript const user = { name: "Tim" }; console.log(Reflect.get(user, "name")); // "Tim" Reflect.set(user, "age", 30); console.log(user.age); // 30 ``` The equivalent: ```javascript user.name; // get user.age = 30; // set ``` But with `Reflect` you can use this **functionally**, passing `target`, `key`, `receiver` by hand. ## Example 2 - removing a property ```javascript 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 ```javascript 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 ```javascript const user = { name: "Tim" }; console.log("name" in user); // true console.log(Reflect.has(user, "name")); // true ``` ## Example 5 - getting the keys ```javascript 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 ```javascript const user = {}; const proto = { greet() { console.log("hi"); } }; Reflect.setPrototypeOf(user, proto); Reflect.getPrototypeOf(user).greet(); // "hi" ``` ## Example 7 - calling a function ```javascript function sum(a, b) { return a + b; } console.log(Reflect.apply(sum, null, [2, 3])); // 5 ``` This is the safe equivalent of `Function.prototype.apply.call(sum, ...)` ## Example 8 - creating instances ```javascript 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) ```javascript 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: age ``` Using `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:** > `Reflect` is a "mirror" of JavaScript's basic operations, in the form of functions. > It makes code cleaner, safer, and more predictable, especially when working with `Proxy`.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.