Movatterモバイル変換


[0]ホーム

URL:


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

handler.get()

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.get() method is a trap for the[[Get]]object internal method, which is used by operations such asproperty accessors.

Try it

const monster = {  secret: "easily scared",  eyeCount: 4,};const handler = {  get(target, prop, receiver) {    if (prop === "secret") {      return `${target.secret.substring(0, 4)} ... shhhh!`;    }    return Reflect.get(...arguments);  },};const proxy = new Proxy(monster, handler);console.log(proxy.eyeCount);// Expected output: 4console.log(proxy.secret);// Expected output: "easi ... shhhh!"

Syntax

js
new Proxy(target, {  get(target, property, receiver) {  }})

Parameters

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

target

The target object.

property

A string orSymbol representing the property name.

receiver

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

Return value

Theget() method can return any value, representing the property value.

Description

Interceptions

This trap can intercept these operations:

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

Invariants

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

  • The value reported for a property must be the same as the value of the corresponding target object property, if the target object property is a non-writable, non-configurable own data property. That is, ifReflect.getOwnPropertyDescriptor() returnsconfigurable: false, writable: false for the property ontarget, then the trap must return the same value as thevalue attribute in thetarget's property descriptor.
  • The value reported for a property must beundefined, if the corresponding target object property is a non-configurable own accessor property that has an undefined getter. That is, ifReflect.getOwnPropertyDescriptor() returnsconfigurable: false, get: undefined for the property ontarget, then the trap must returnundefined.

Examples

Trap for getting a property value

The following code traps getting a property value.

js
const p = new Proxy(  {},  {    get(target, property, receiver) {      console.log(`called: ${property}`);      return 10;    },  },);console.log(p.a);// "called: a"// 10

The following code violates an invariant.

js
const obj = {};Object.defineProperty(obj, "a", {  configurable: false,  enumerable: false,  value: 10,  writable: false,});const p = new Proxy(obj, {  get(target, property) {    return 20;  },});p.a; // TypeError is thrown

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-proxy-object-internal-methods-and-internal-slots-get-p-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