Proxy

Proxy

Creates a new Proxy object that wraps a target object and intercepts operations defined by a handler

Syntax

JavaScript
new Proxy(target, handler)

Parameters

ParameterTypeDescription
targetobjectThe target object to wrap
handlerProxyHandlerAn object with trap functions for intercepting operations

Return Value

A new Proxy object

Examples

Basic Usage
const target = { name: 'Alice', age: 30 }
const proxy = new Proxy(target, {
  get(obj, prop) {
    console.log(`Accessing ${String(prop)}`)
    return Reflect.get(obj, prop)
  }
})
console.log(proxy.name) // logs 'Accessing name', then 'Alice'
Practical Example
function createReactive<T extends object>(obj: T, onChange: () => void): T {
  return new Proxy(obj, {
    set(target, prop, value) {
      const result = Reflect.set(target, prop, value)
      onChange()
      return result
    }
  })
}
Advanced Usage
const validator = new Proxy({} as Record<string, number>, {
  set(obj, prop, value) {
    if (typeof value !== 'number') throw new TypeError('Value must be a number')
    return Reflect.set(obj, prop, value)
  }
})
validator.x = 42 // OK
// validator.y = 'hello' // throws TypeError

Understanding Proxy

The Proxy method in JavaScript creates a new Proxy object that wraps a target object and intercepts operations defined by a handler. It belongs to the Proxy object and is one of the most widely used methods for working with proxy values in modern JavaScript and TypeScript applications.

The method signature is new Proxy(target, handler). It accepts 2 parameters: target, handler. When called, it returns a new proxy object. Understanding when and how to use Proxy() helps you write more expressive, readable code.

Common use cases for Proxy include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like proxy-revocable, reflect-get, reflect-set, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for Proxy is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More Proxy Methods

Other methods in the Proxy object

Related Tools

More Proxy Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.