简体   繁体   English

object Object.defineProperty()执行后不显示新增属性

[英]object not showing the newly added property after Object.defineProperty() executed

 var obj = { name: "dev", age: 27, } obj = Object.defineProperty(obj, 'mail', { value: 'dev@hmail.com', writable: true }); console.log(obj) // Printing the object with old properties ie.{ name: 'dev', age: 27 }}

With the above code it is showing only the old properties.使用上面的代码,它只显示旧属性。 Why does it not showing the new property "mail"?为什么它不显示新属性“邮件”?

In the documentation for Object.defineProperty it is saidObject.defineProperty 的文档中说

By default, values added using Object.defineProperty() are immutable and not enumerable .默认情况下,使用 Object.defineProperty() 添加的值是不可变且不可枚举的。

If you print an object with console.log(obj) though, there might be the possibility, that only the enumerable properties are printed (depends on the implementation of console.log).但是,如果您使用console.log(obj)打印 object,则可能只打印可枚举的属性(取决于 console.log 的实现)。 The defined member is still there, but just does not show up.定义的成员仍然存在,只是没有出现。

Printing the member directly works:直接打印成员有效:

 var obj = { name: "dev", age: 27, }; obj = Object.defineProperty(obj, 'mail', { value: 'dev@hmail.com', writable: true }); console.log(obj.mail);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM