The difference between a custom attribute and the ability to write object data data

I saw the following regarding javscript attribute property attributes, objects

- Custom attribute: Indicates whether the property can be deleted or changed. - Enumerable: Indicates whether a property can be returned in a for / in loop. - Writable: Indicates whether the property can be changed.

here "Configurable attribute" and "Writable" represent the same thing (is it possible to change the property), then why do we need two separate attributes?

+6
source share
4 answers

From: http://ejohn.org/blog/ecmascript-5-objects-and-properties/

Writable: If false, the property value cannot be changed.

Configurable: if false, any attempt to delete a property or change its attributes (Writable, Configurable or Enumerable) will fail.

Enumerable: If true, the property will be repeated when the user executes (var prop in obj) {} (or similar).

+10
source

Customizable prevents any attempts to "override" key properties using Object.defineProperty , chrome will display an error sign

Uncaught TypeError: cannot override property: foo

A writable attribute just avoids editing this value

+2
source

configurable and writable NOT represent the same thing.

configurable means property descriptor and existence.

writable means only the value of the property.

The property descriptor contains a value that is enumerable, customizable, and writable.

scenario 1 : creating a property on a job

 'use strict'; // non-strict mode behaves slightly different var foo = {}; foo.bar = 1; // operated by CreateDataProperty* // the above is the same as Object.defineProperty(foo, 'bar', { value: 1, configurable: true, writable: true, // ... }); 

scenario 2 : creating a property by handle

 'use strict'; // non-strict mode behaves slightly different var foo = {}; Object.defineProperty(foo, 'bar', { value: 1, // configurable => false // writable => false }); foo.bar = 2; // throw TypeError: Cannot assign to read only property Object.defineProperty(foo, 'bar', { value: 2 // ... }); // throw TypeError: Cannot redefine property delete foo.bar; // throw TypeError: Cannot delete property 
+2
source

If the Writable parameter is set to true, the value of the object property can be changed.

If the Configurable parameter is set to true, means that the type of the property of the object can be changed from the data property to the accessor property (or vice versa); and the property of the object can be removed.

0
source

All Articles