【发布时间】:2021-11-15 12:54:37
【问题描述】:
我试图了解 JavaScript 对象是如何充分发挥作用的。我知道当你在一个对象上调用Object.getOwnPropertyDescriptors 时,你会得到一个包含描述符的对象。例如,如果您定义了自己的对象,并获得了描述符,您将得到如下内容:
let foo = {
bar: 2
}
Object.getOwnPropertyDescriptors(foo);
// Output:
// {
// bar: {
// value: 2,
// writable: true,
// enumerable: true,
// configurable: true,
// }
// }
同样,如果您在内置对象上获取描述符,例如像Error,你可以看到错误对象的描述符。
let foo = new Error();
Object.getOwnPropertyDescriptors(foo);
// Output:
// {
// bar: {
// value: 'Error\n at <anonymous>:1:11',
// writable: true,
// enumerable: false,
// configurable: true,
// }
// }
我了解该属性是在调用 Error 构造函数时创建的。我刚才所说的一切对我来说都是有意义的。但是,当我尝试获取日期对象的描述符时,没有返回任何内容。
let foo = new Date();
Object.getOwnPropertyDescriptors(foo);
// Output:
// {}
我可以通过访问它以字符串形式获取日期的值,但是为什么没有该值的描述符?
【问题讨论】:
标签: javascript object types descriptor