The handler.get()
method is a trap for the [[Get]]
object internal method, which is used by operations such as property accessors.
The handler.get()
method is a trap for the [[Get]]
object internal method, which is used by operations such as property accessors.
new Proxy(target, { get(target, property, receiver) { } });
The get()
method can return any value.
This trap can intercept these operations:
proxy[foo]
and proxy.bar
Reflect.get()
Or any other operation that invokes the [[Get]]
internal method.
If the following invariants are violated, the trap throws a TypeError
when invoked.
undefined
as its [[Get]]
attribute. The following code traps getting a property value.
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.
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
Specification |
---|
ECMAScript Language Specification # sec-proxy-object-internal-methods-and-internal-slots-get-p-receiver |
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | ||
get |
49 | 12 | 18 | 36 | 10 | 49 | 18 | 36 | 10 | 5.0 | 49 | 1.0 | 6.0.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/get