JavaScript Reference ยท Object
Property descriptors
Concept reference for JavaScript property descriptors: writable, enumerable, configurable, get and set, defaults, and how freeze and seal use them.
Every property of an object has a descriptor: a small record of attributes that controls how the property behaves. You normally never see it, because assignment and object literals fill it in with permissive defaults. Object.defineProperty() and Object.getOwnPropertyDescriptor() let you read and change it.
There are two kinds of properties. A data property stores a value. An accessor property runs a getter and/or setter function instead.
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
The attributes
| Attribute | Applies to | Meaning |
|---|---|---|
value |
data | The stored value. Default undefined when defining. |
writable |
data | If false, assignment does not change the value. |
get |
accessor | A function called when the property is read. |
set |
accessor | A function called when the property is assigned. |
enumerable |
both | If true, the property appears in for...in, Object.keys(), spread and JSON.stringify(). |
configurable |
both | If false, the property cannot be deleted and its attributes cannot be changed (with narrow exceptions). |
A descriptor is either a data descriptor (value/writable) or an accessor descriptor (get/set). Mixing them in one call throws a TypeError.
Defaults
Properties created by assignment, literals or class fields are writable: true, enumerable: true, configurable: true. Properties created through Object.defineProperty() default every missing flag to false (and value to undefined). That difference surprises many people. When you redefine an existing property, flags you omit keep their current values instead.
Failure behavior
Violating an attribute (assigning to a read-only property, deleting a non-configurable one) fails silently in sloppy mode and throws a TypeError in strict mode, modules and classes.
What the integrity methods do
| Method | Effect on descriptors |
|---|---|
Object.preventExtensions() |
No change to existing properties; blocks new ones. |
Object.seal() |
Sets configurable: false on every property. |
Object.freeze() |
Also sets writable: false on data properties. Accessors keep working. |
Usage notes
- Non-configurable is mostly one-way. The exceptions are changing
writablefromtruetofalse, and changing the value of a writable property. - Hiding helpers. Non-enumerable properties keep metadata out of loops and serialization while remaining readable.
- Computed values: accessor properties let a property look like data while running code, as with
get formatted()above. - Copying loses accessors' behavior. Spread and
Object.assign()read getters and copy the resulting value. To copy descriptors faithfully, useObject.defineProperties(target, Object.getOwnPropertyDescriptors(source)). - Inherited properties have descriptors on the prototype object, not on the instance;
Object.getOwnPropertyDescriptor()returnsundefinedfor them. - Symbols can have descriptors just like strings.
Browser support
Property descriptors and the related Object methods are available in every browser and JavaScript runtime.