handler.get()
method is a trap for getting a property value.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
const p = new Proxy(target, {
get: function(target, property, receiver) {
}
});
The following parameters are passed to the
get()
方法。
this
is bound to the handler.
target
The target object.
property
Symbol
of the property to get.
receiver
Either the proxy or an object that inherits from the proxy.
get()
method can return any value.
handler.get()
method is a trap for getting a property value.
This trap can intercept these operations:
proxy
[
foo
]
and
proxy
.
bar
Object.create(
proxy
)[
foo
]
Reflect.get()
If the following invariants are violated, the proxy will throw a
TypeError
:
undefined
as its
[[Get]]
属性。
The following code traps getting a property value.
const p = new Proxy({}, {
get: function(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: function(target, property) {
return 20;
}
});
p.a; // TypeError is thrown
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of '[[Get]]' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
get
|
Chrome 49 | Edge 12 | Firefox 18 | IE No | Opera 36 | Safari 10 | WebView Android 49 | Chrome Android 49 | Firefox Android 18 | Opera Android 36 | Safari iOS 10 | Samsung Internet Android 5.0 | nodejs 6.0.0 |
完整支持
不支持