静态方法 Reflect.getOwnPropertyDescriptor() 与 undefined。
语法
Reflect.getOwnPropertyDescriptor(target, propertyKey)
参数
-
target - 需要寻找属性的目标对象。
-
propertyKey - 获取自己的属性描述符的属性的名称。
返回值
如果属性存在于给定的目标对象中,则返回属性描述符;否则,返回 undefined。
异常
抛出一个 Object。
描述
Reflect.getOwnPropertyDescriptor方法返回一个属性描述符,如果给定的属性存在于对象中,否则返回 Object.getOwnPropertyDescriptor() 的唯一不同在于如何处理非对象目标。
示例
使用 Reflect.getOwnPropertyDescriptor()
Reflect.getOwnPropertyDescriptor({x: "hello"}, "x");
// {value: "hello", writable: true, enumerable: true, configurable: true}
Reflect.getOwnPropertyDescriptor({x: "hello"}, "y");
// undefined
Reflect.getOwnPropertyDescriptor([], "length");
// {value: 0, writable: true, enumerable: false, configurable: false}
与 Object.getOwnPropertyDescriptor() 的不同点
如果该方法的第一个参数不是一个对象(一个原始值),那么将造成 Object.getOwnPropertyDescriptor,非对象的第一个参数将被强制转换为一个对象处理。
Reflect.getOwnPropertyDescriptor("foo", 0);
// TypeError: "foo" is not non-null object
Object.getOwnPropertyDescriptor("foo", 0);
// { value: "f", writable: false, enumerable: true, configurable: false }
规范
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) Reflect.getOwnPropertyDescriptor |
Standard | Initial definition. |
| ECMAScript Latest Draft (ECMA-262) Reflect.getOwnPropertyDescriptor |
Draft |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains.
Find out how you can help!
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 49 | 42 (42) | 未实现 | 未实现 | 10 |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | 未实现 | 49 | 42.0 (42) | 未实现 | 未实现 | 10 |