handler.getOwnPropertyDescriptor()
method is a trap for
Object.getOwnPropertyDescriptor()
.
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, {
getOwnPropertyDescriptor: function(target, prop) {
}
});
The following parameters are passed to the
getOwnPropertyDescriptor()
方法。
this
is bound to the handler.
target
The target object.
prop
The name of the property whose description should be retrieved.
getOwnPropertyDescriptor()
method must return an object or
undefined
.
handler.getOwnPropertyDescriptor()
method is a trap for
Object.getOwnPropertyDescriptor()
.
This trap can intercept these operations:
If the following invariants are violated, the proxy will throw a
TypeError
:
getOwnPropertyDescriptor()
must return an object or
undefined
.
Object.getOwnPropertyDescriptor(
target
)
can be applied to the target object using
Object.defineProperty()
and will not throw an exception.
The following code traps
Object.getOwnPropertyDescriptor()
.
const p = new Proxy({ a: 20}, {
getOwnPropertyDescriptor: function(target, prop) {
console.log('called: ' + prop);
return { configurable: true, enumerable: true, value: 10 };
}
});
console.log(Object.getOwnPropertyDescriptor(p, 'a').value); // "called: a"
// 10
The following code violates an invariant.
const obj = { a: 10 };
Object.preventExtensions(obj);
const p = new Proxy(obj, {
getOwnPropertyDescriptor: function(target, prop) {
return undefined;
}
});
Object.getOwnPropertyDescriptor(p, 'a'); // TypeError is thrown
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of '[[GetOwnProperty]]' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
getOwnPropertyDescriptor
|
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 |
完整支持
不支持