调用 “defineProperties” 后,我的对象变为空。

huangapple go评论78阅读模式
英文:

Why after calling "defineProperties", my object became empty

问题

我有这个对象

const product = { 
    name: "Laptop", 
    price: 1000, 
    quantity: 5 
};
console.log(product) //{ name: 'Laptop', price: 1000, quantity: 5 }

但是之后我应该这样做:

const descriptors = { enumerable: false, writable: false };

Object.defineProperties(product, {
    price: { ...descriptors },
    quantity: { ...descriptors },
});

在控制台中,使用defineProperties后我得到了这个:

console.log(product) //{ name: 'Laptop' }

我的问题:为什么会这样?

我期望产品对象将保持不变,只是在价格和数量上禁用可枚举和可写属性,并且不理解它是如何工作的(defineProperties)
我还尝试过像这样写它:"price:{ enumerable: false, writable: false }",但结果相同。

英文:

I have this object

const product = { 
    name: "Laptop", 
    price: 1000, 
    quantity: 5 
  };
console.log(product) //{ name: 'Laptop', price: 1000, quantity: 5 }

but then I should to do this:

const descriptors = { enumerable: false, writable: false };

Object.defineProperties(product, {
    price: { ...descriptors },
    quantity: { ...descriptors },
});

and in console I get this after using defineProperties:

console.log(product) //{ name: 'Laptop' }

My question: Why it's happening?

I expected that the product object will the same but with disables in enumerable and writable only in price and quantity and didn't understand how it works(defineProperties)
Also tried to write it's like "price:{ enumerable: false, writable: false }" the same result

答案1

得分: 1

使用Object.defineProperties()时,你正在定义要修改的对象属性的属性描述符。

将Object.defineProperties()应用于具有价格和数量描述符的产品后,这两个属性都变为不可枚举且只读。因此,当你记录产品对象时,只会显示名称属性,因为它是唯一可枚举的属性。价格和数量属性仍然存在于对象中,但它们不会显示在控制台中,也无法在循环中访问,并且无法直接更改它们的值。

如果尝试直接访问价格和数量属性,你会发现它们仍然存在,但不可写:

console.log(product.price); // 1000
console.log(product.quantity); // 5
英文:

When you use Object.defineProperties(), you are defining the property descriptors for the object properties you want to modify.

After applying Object.defineProperties() to product with the descriptors for price and quantity, both properties become non-enumerable and read-only. As a result, when you log the product object, only the name property is shown because it is the only enumerable property. The price and quantity properties are still present in the object, but they won't show up in the console or be accessible in loops, and you won't be able to change their values directly.

if you try to access the price and quantity properties directly, you'll find that they are still there but are not writable:

console.log(product.price); // 1000
console.log(product.quantity); // 5

答案2

得分: 0

你将enumerable设置为false。因此,任何尝试枚举键或条目的操作都不会看到它。

英文:

You're setting enumerable to false. Therefore anything that tries to enumerate keys or entries won't see it.

huangapple
  • 本文由 发表于 2023年8月4日 00:53:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830139.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定