简体   繁体   English

如果writable属性默认为false,JS对象如何可变

[英]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 :{}中所述

  1. 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.

相关问题 如何为对象属性的可变引用赋值? - How to assign a value to a mutable reference of an object property? 如何在JS中创建对象的可变且不可变的副本 - How to create mutable AND immutable copy of object in JS 如何更改Javascript对象属性的可写可枚举和可配置值? - How to change writable enumerable and configurable values of a Javascript object property? 为什么JavaScript Object 的属性已经是configurable:false,writable 总是可以从true 变为false 而不会出错? - Why JavaScript Object's property is already configurable:false, writable can always be changed from true to false without error? 如何使JS导入可写? - How to make JS imports writable? 如何解决错误 Cannot assign to read only property 'defaults' of object '[object Object]' in react and grapesjs? - How to resolve error Cannot assign to read only property 'defaults' of object '[object Object]' in react and grapesjs? 有2个具有相同id的对象数组,但一个对象属性为真,另一个属性为假,如果属性为假,如何删除两者 - There is 2 array of object with same id, but one object property is true and other property is false, how to remove both if property is false Node.js writable.write返回false? - Node.js writable.write return false? JavaScript“可写”属性描述符如何工作? - How does JavaScript “writable” property descriptor work? 在JS中解构时嵌套部分object默认值? - Nested partial object defaults when destructuring in JS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM