Movatterモバイル変換


[0]ホーム

URL:


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

handler.set()

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⁩.

Thehandler.set() method is a trap for the[[Set]]object internal method, which is used by operations such as usingproperty accessors to set a property's value.

Try it

const monster = { eyeCount: 4 };const handler = {  set(obj, prop, value) {    if (prop === "eyeCount" && value % 2 !== 0) {      console.log("Monsters must have an even number of eyes");    } else {      return Reflect.set(...arguments);    }  },};const proxy = new Proxy(monster, handler);proxy.eyeCount = 1;// Expected output: "Monsters must have an even number of eyes"console.log(proxy.eyeCount);// Expected output: 4proxy.eyeCount = 2;console.log(proxy.eyeCount);// Expected output: 2

Syntax

js
new Proxy(target, {  set(target, property, value, receiver) {  }})

Parameters

The following parameters are passed to theset() method.this is bound to the handler.

target

The target object.

property

A string orSymbol representing the property name.

value

The new value of the property to set.

receiver

Thethis value for setters; seeReflect.set(). This is usually either the proxy itself or an object that inherits from the proxy.

Return value

Theset() method must return aBoolean indicating whether or not the assignment succeeded. Other values arecoerced to booleans.

Many operations, including using property accessors instrict mode, throw aTypeError if the[[Set]] internal method returnsfalse.

Description

Interceptions

This trap can intercept these operations:

  • Property assignment:proxy[foo] = bar andproxy.foo = bar
  • Reflect.set()

Or any other operation that invokes the[[Set]]internal method.

Invariants

The proxy's[[Set]] internal method throws aTypeError if the handler definition violates one of the following invariants:

  • Cannot change the value of a property to be different from the value of the corresponding target object property, if the corresponding target object property is a non-writable, non-configurable own data property. That is, ifReflect.getOwnPropertyDescriptor() returnsconfigurable: false, writable: false for the property ontarget, andvalue is different from thevalue attribute in thetarget's property descriptor, then the trap must return a falsy value.
  • Cannot set the value of a property if the corresponding target object property is a non-configurable own accessor property that has an undefined setter. That is, ifReflect.getOwnPropertyDescriptor() returnsconfigurable: false, set: undefined for the property ontarget, then the trap must return a falsy value.

Examples

Trap setting of a property value

The following code traps setting a property value.

js
const p = new Proxy(  {},  {    set(target, prop, value, receiver) {      target[prop] = value;      console.log(`property set: ${prop} = ${value}`);      return true;    },  },);console.log("a" in p); // falsep.a = 10; // "property set: a = 10"console.log("a" in p); // trueconsole.log(p.a); // 10

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-proxy-object-internal-methods-and-internal-slots-set-p-v-receiver

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp