Object.getOwnPropertyDescriptor
Object.getOwnPropertyDescriptor()
方法可以获取对象自有 Property 的某个 Attributes。
语法
语法:
Object.getOwnPropertyDescriptor(o, property);
类型声明:
declare type PropertyKey = string | number | symbol;
interface PropertyDescriptor {
configurable?: boolean;
enumerable?: boolean;
value?: any;
writable?: boolean;
get?(): any;
set?(v: any): void;
}
interface ObjectConstructor {
getOwnPropertyDescriptor(o: any, p: PropertyKey): PropertyDescriptor | undefined;
}
参数说明:
参数 | 说明 | 类型 |
---|---|---|
o | 需要查找的目标对象 | object |
property | 目标对象的 Property | string |
代码示例
const foo = { a: 1 };
Object.getOwnPropertyDescriptor(foo, 'a');
// Object {
// value: "a",
// writable: true,
// enumerable: true,
// configurable: true,
// }