[英]Strange behavior of Object.defineProperty() in JavaScript
I was playing with below javascript code.我在玩下面的javascript代码。 Understanding of
Object.defineProperty()
and I am facing a strange issue with it.对
Object.defineProperty()
的理解,我遇到了一个奇怪的问题。 When I try to execute below code in the browser or in the VS code the output is not as expected whereas if I try to debug the code the output is correct当我尝试在浏览器或 VS 代码中执行以下代码时,输出与预期不符,而如果我尝试调试代码,则输出是正确的
When I debug the code and evaluate the profile I can see the name & age
property in the object But at the time of output, it only shows the name
property当我调试代码并评估配置文件时,我可以在对象中看到
name & age
属性但是在输出时,它只显示name
属性
//Code Snippet let profile = { name: 'Barry Allen', } // I added a new property in the profile object. Object.defineProperty(profile, 'age', { value: 23, writable: true }) console.log(profile) console.log(profile.age)
Now expected output here should be现在这里的预期输出应该是
{name: "Barry Allen", age: 23}
23
but I get the output as.但我得到的输出。 Note that I am able to access the
age
property defined afterwards.请注意,我可以访问之后定义的
age
属性。 I am not sure why the console.log()
is behaving this way.我不确定为什么
console.log()
会这样。
{name: "Barry Allen"}
23
You should set enumerable<\/code> to
true<\/code> .
您应该将
enumerable<\/code>设置为
true<\/code> 。
In
Object.defineProperty<\/code> its
false<\/code> by default.
在
Object.defineProperty<\/code>中默认为
false<\/code> 。
According to MDN<\/a> .
根据
MDN<\/a> 。
<\/blockquote>
To get all properties(including non-enumerable)
Object.getOwnPropertyNames()<\/code><\/a> .
获取所有属性(包括不可枚举的)
Object.getOwnPropertyNames()<\/code><\/a> 。
"
By default, properties you define with defineProperty<\/code> are not enumerable<\/em> - this means that they will not show up when you iterate over their
Object.keys<\/code> (which is what the snippet console does).
默认情况下,您使用
defineProperty<\/code>定义的属性是不可枚举<\/em>的——这意味着当您遍历它们的
Object.keys<\/code> (这是代码段控制台所做的)时,它们不会显示。
(Similarly, the
length<\/code> property of an array does not get displayed, because it's non-enumerable.)
(同样,数组的
length<\/code>属性也不会显示,因为它是不可枚举的。)
enumerable<\/strong>可枚举的<\/strong>
Defaults to false.<\/strong>默认为假。<\/strong>
<\/blockquote>
Make it enumerable instead:改为可枚举:
The reason you can see the property in the logged image<\/a> is that Chrome's console will show you non-enumerable properties as well - but the non-enumerable properties will be slightly greyed-out<\/strong> :您可以在
记录的图像<\/a>中看到该属性的原因是 Chrome 的控制台也会向您显示不可枚举的属性 -但不可枚举的属性将略微变灰<\/strong>:
"
Whenever you use".defineProperty" method of object.每当您使用对象的“.defineProperty”方法时。 You should better define all the properties of the descriptor.
您应该更好地定义描述符的所有属性。 Because if you don't define other property descriptor then it assumes default values for all of them which is false.
因为如果您不定义其他属性描述符,那么它会假定所有这些属性的默认值都是错误的。 So your console.log checks for all the enumerable : true properties and logs them.
因此,您的 console.log 会检查所有 enumerable : true 属性并记录它们。
//Code Snippet
let profile = {
name: 'Barry Allen',
}
// I added a new property in the profile object.
Object.defineProperty(profile, 'age', {
value: 23,
writable: true,
enumerable : true,
configurable : true
})
console.log(profile)
console.log(profile.age)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.