What are JavaScript Proxy and Reflect objects
· Category: JavaScript
Short answer
A Proxy wraps an object and intercepts operations like property access, assignment, and function calls via handler traps. Reflect provides the default implementations of those operations, making it easy to forward behavior from custom traps.
Details
Proxies are powerful for validation, logging, and reactive data systems. For example, a set trap can validate that a property is always a number before writing it. The corresponding Reflect.set(target, property, value) performs the actual write without recursion back into the proxy.
Because proxies can change expected object behavior, they should be used carefully in large codebases. If you are building reactive UIs with proxies, you might also want to understand how to use the Storage API for client-side data for persisting reactive state across sessions.
Tips
- Proxies do not work with all object types equally;
Dateand some DOM objects may behave unexpectedly when wrapped. - Use
Reflectmethods inside traps to preserve the correctthisbinding and return values. - For more advanced object manipulation, see how to destructure objects and arrays as a companion to reading and writing proxied properties.