简体   繁体   English

"JavaScript 中 Object.defineProperty() 的奇怪行为"

[英]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> 。

enumerable<\/strong>可枚举的<\/strong>

true<\/code> if and only if this property shows up during enumeration of the properties on the corresponding object.当且仅当在枚举相应对象的属性期间显示此属性时才为true<\/code> 。

Defaults to false.<\/strong>默认为假。<\/strong>

<\/blockquote>

Non-enumerable means that property will not be shown in Object.keys()<\/code> or for..in<\/code> loop neither in console不可枚举意味着属性不会在Object.keys()<\/code>或for..in<\/code>循环中显示,也不会在控制台中显示

All the properties and methods on prototype<\/code> object of built-in classes are non-enumerable.内置类的prototype<\/code>对象上的所有属性和方法都是不可枚举的。 Thats is the reason you can call them from instance but they don't appear while iterating.这就是您可以从实例调用它们但它们在迭代时不会出现的原因。

To get all properties(including non-enumerable) Object​.get​OwnProperty​Names()<\/code><\/a> .获取所有属性(包括不可枚举的) Object​.get​OwnProperty​Names()<\/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>属性也不会显示,因为它是不可枚举的。)

See MDN<\/a> :MDN<\/a> :

enumerable<\/strong>可枚举的<\/strong>

true if and only if this property shows up during enumeration of the properties on the corresponding object.当且仅当在枚举相应对象的属性期间显示此属性时才为 true。

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>:

在此处输入图像描述<\/a>

See how age<\/code> is grey-ish, while name<\/code> is not - this indicates that name<\/code> is enumerable, and age<\/code> is not.看看age<\/code>是灰色的,而name<\/code>不是 - 这表明name<\/code>是可枚举的,而age<\/code>不是。

"

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.

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