handler.getOwnPropertyDescriptor()
handler.getOwnPropertyDescriptor()
方法是对 [[GetOwnProperty]]
对象内部方法 的陷阱,被 Object.getOwnPropertyDescriptor()
等操作使用。
¥The handler.getOwnPropertyDescriptor()
method is a trap for the [[GetOwnProperty]]
object internal method, which is used by operations such as Object.getOwnPropertyDescriptor()
.
Try it
语法
参数
返回值
¥Return value
getOwnPropertyDescriptor()
方法必须返回一个对象或 undefined
,表示属性描述符。缺失属性的规范化方式与 Object.defineProperty()
相同。
¥The getOwnPropertyDescriptor()
method must return an object or undefined
, representing the property descriptor. Missing attributes are normalized in the same way as Object.defineProperty()
.
描述
拦截
¥Interceptions
该陷阱可以拦截以下操作:
¥This trap can intercept these operations:
或调用 [[GetOwnProperty]]
内部方法 的任何其他操作。
¥Or any other operation that invokes the [[GetOwnProperty]]
internal method.
不变量
¥Invariants
如果处理程序定义违反以下不变量之一,则代理的 [[GetOwnProperty]]
内部方法将抛出 TypeError
:
¥The proxy's [[GetOwnProperty]]
internal method throws a TypeError
if the handler definition violates one of the following invariants:
- 结果必须是
Object
或undefined
。 - 如果属性作为目标对象的不可配置自有属性存在,则不能将其报告为不存在。也就是说,如果
Reflect.getOwnPropertyDescriptor()
对target
上的属性返回configurable: false
,则陷阱不得返回undefined
。 - 如果属性作为不可扩展目标对象的自有属性而存在,则不能将其报告为不存在。也就是说,如果
Reflect.isExtensible()
对目标对象返回false
,则陷阱不得返回undefined
。 - 如果某个属性不是目标对象的自有属性,并且目标对象不可扩展,则不能将该属性报告为存在。也就是说,如果
Reflect.isExtensible()
对目标对象返回false
,而Reflect.getOwnPropertyDescriptor()
对target
上的属性返回undefined
,则陷阱必须返回undefined
。 - 除非某个属性是目标对象的不可配置自有属性,则不能将该属性报告为不可配置。也就是说,如果
Reflect.getOwnPropertyDescriptor()
对target
上的属性返回undefined
或configurable: true
,则陷阱不得返回configurable: false
。 - 除非某个属性是目标对象的不可配置、不可写的自有属性,否则不能将该属性报告为既不可配置又不可写。也就是说,除了前面的不变量之外,如果
Reflect.getOwnPropertyDescriptor()
对target
上的属性返回configurable: false, writable: true
,则陷阱不得返回configurable: false, writable: false
。 - 如果某个属性在目标对象上具有相应的属性,则目标对象属性的描述符必须与
descriptor
兼容。也就是说,假装target
是一个普通对象,则Object.defineProperty(target, property, resultObject)
不得抛出错误。Object.defineProperty()
参考包含更多信息,但总结一下,当目标属性不可配置时,必须满足以下条件:configurable
、enumerable
、get
和set
必须与原始值相同。writable
也必须是原始的,这是由于前一个不变量。- 属性必须保留为数据或访问器
- 只有当
writable
为true
时,才能更改value
属性
示例
getOwnPropertyDescriptor 的捕获
¥Trapping of getOwnPropertyDescriptor
以下代码捕获 Object.getOwnPropertyDescriptor()
。
¥The following code traps Object.getOwnPropertyDescriptor()
.
const p = new Proxy(
{ a: 20 },
{
getOwnPropertyDescriptor(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(target, prop) {
return undefined;
},
});
Object.getOwnPropertyDescriptor(p, "a"); // TypeError is thrown
规范
Specification |
---|
ECMAScript Language Specification # sec-proxy-object-internal-methods-and-internal-slots-getownproperty-p |
浏览器兼容性
BCD tables only load in the browser