[英]How is the JS object mutable if the writable property defaults to false
I am trying to understand how the Object mutation in JS works. 我试图了解JS中的Object变异是如何工作的。 I came across the writable property on JS.
我在JS上遇到了可写属性。
var x = {name:'JohnSnow'};
Object.defineProperty(x, 'name', {
writable: false
};
Now changing the name
property has no effect on the object x
. 现在更改
name
属性对对象x
没有影响。 This is understandable as I have changed its writable
property to false
. 这是可以理解的,因为我已将其
writable
属性更改为false
。
However, all the docs that I read including ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties#Parameters ) says that this property is false
by default. 但是,我阅读的所有文档( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties#Parameters )都表示默认情况下此属性为
false
。
If the writable
is set to false
by default, shouldn't the object be immutable by default and should become mutable only after setting writable
to true
. 如果默认情况下将
writable
设置为false
,则默认情况下该对象不应该是不可变的,并且只有在将writable
设置为true
后才能变为可变。
Am I wrong in understanding the concept of Object properties here, because I think all the objects in JS have this config
and every object has writable
property. 我在这里理解对象属性的概念是错误的,因为我认为JS中的所有对象都有这个
config
并且每个对象都有writable
属性。
Object.defineProperty
should usually only be used when defining a new property - otherwise, the property descriptor object passed will often be partially ignored, as described in step 2 of 9.1.6.3 ValidateAndApplyPropertyDescriptor . 通常只应在定义新属性时使用
Object.defineProperty
- 否则,传递的属性描述符对象通常会被部分忽略,如9.1.6.3 ValidateAndApplyPropertyDescriptor的步骤2中所述 。
In step 2, the new descriptor, desc
, will only be put as the new property descriptor if the current descriptor is undefined. 在步骤2中,如果当前描述符未定义,则新描述符
desc
将仅作为新属性描述符。
In step 6, an existing data property might be switched to become an accessor property (or the other way around), but 在步骤6中,可以将现有数据属性切换为访问者属性(或相反),但是
Preserve the existing values of the converted property's [[Configurable]] and [[Enumerable]] attributes and set the rest of the property's attributes to their default values .
保留已转换属性的[[Configurable]]和[[Enumerable]]属性的现有值,并将属性的其余属性设置为其默认值 。
Steps 7 and 8 do not change the current descriptor, they simply verify and return a value. 步骤7和8不会更改当前描述符,它们只是验证并返回一个值。
So, no matter the descriptor passed, if the property already exists on the object, the writable
property (or its absence) of the "new" descriptor will be completely ignored. 因此,无论传递的描述符如何,如果该对象上已存在该属性,那么“new”描述符的
writable
属性(或其缺失)将被完全忽略。
writable
does default to false
, but only when using Object.defineProperty
. writable
默认为false
,但仅在使用Object.defineProperty
。
var x = {}; Object.defineProperty(x, 'name', { value: 'foo' }); x.name = 'bar'; console.log(x.name);
As you can see above, if you do not provide a writable
property when calling Object.defineProperty
, it will default to false
. 如上所示,如果在调用
Object.defineProperty
时未提供writable
属性,则默认为false
。 That's all it means - it's not specifying that properties are always non-writable by default, it's only specifying that properties are non-writable when defining a property with Object.defineProperty
. 这一切都意味着-这不是指定属性始终是在默认情况下不可写,它只是指定的属性定义与物业时不可写
Object.defineProperty
。
Object properties are writable when defining an object with an object literal, described in 12.2.6.7 Runtime Semantics: Evaluation ObjectLiteral : {} : 在定义具有对象文字的对象时,对象属性是可写的,如12.2.6.7运行时语义:评估ObjectLiteral :{}中所述 :
- Perform ?
表演? PropertyDefinitionEvaluation of PropertyDefinitionList with arguments obj and true.
PropertyDefinition使用参数obj和true评估PropertyDefinitionList。
Which eventually leads you to: 最终会引导您:
CreateDataProperty ( O, P, V ) : CreateDataProperty(O,P,V) :
Let newDesc be the PropertyDescriptor { [[Value]]: V, [[Writable]]: true , [[Enumerable]]: true, [[Configurable]]: true }.
设newDesc为PropertyDescriptor {[[Value]]:V, [[Writable]]:true ,[[Enumerable]]:true,[[Configurable]]:true}。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.