Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Proxy
  6. Proxy()

Proxy() constructor

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨September 2016⁩.

TheProxy() constructor createsProxy objects.

Syntax

js
new Proxy(target, handler)

Note:Proxy() can only be constructed withnew. Attempting to call it withoutnew throws aTypeError.

Parameters

target

A target object to wrap withProxy. It can be any sort of object,including a native array, a function, or even another proxy.

handler

An object whose properties are functions that define the behavior of the proxy whenan operation is performed on it.

Description

Use theProxy() constructor to create a newProxy object.This constructor takes two mandatory arguments:

  • target is the object for which you want to create the proxy
  • handler is the object that defines the custom behavior of the proxy.

An empty handler will create a proxy that behaves, in almost all respects, exactly likethe target. By defining any of a set group of functions on thehandlerobject, you can customize specific aspects of the proxy's behavior. For example, bydefiningget() you can provide a customized version of the target'sproperty accessor.

Handler functions

This section lists all the handler functions you can define. Handler functions aresometimes calledtraps, because they trap calls to the underlying targetobject.

handler.apply()

A trap for a function call.

handler.construct()

A trap for thenew operator.

handler.defineProperty()

A trap forObject.defineProperty.

handler.deleteProperty()

A trap for thedelete operator.

handler.get()

A trap for getting property values.

handler.getOwnPropertyDescriptor()

A trap forObject.getOwnPropertyDescriptor.

handler.getPrototypeOf()

A trap forObject.getPrototypeOf.

handler.has()

A trap for thein operator.

handler.isExtensible()

A trap forObject.isExtensible.

handler.ownKeys()

A trap forObject.getOwnPropertyNames andObject.getOwnPropertySymbols.

handler.preventExtensions()

A trap forObject.preventExtensions.

handler.set()

A trap for setting property values.

handler.setPrototypeOf()

A trap forObject.setPrototypeOf.

Examples

Selectively proxy property accessors

In this example the target has two properties,notProxied andproxied. We define a handler that returns a different value forproxied, and lets any other accesses through to the target.

js
const target = {  notProxied: "original value",  proxied: "original value",};const handler = {  get(target, prop, receiver) {    if (prop === "proxied") {      return "replaced value";    }    return Reflect.get(...arguments);  },};const proxy = new Proxy(target, handler);console.log(proxy.notProxied); // "original value"console.log(proxy.proxied); // "replaced value"

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-proxy-constructor

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp