Glossary / Object property descriptors
Object property descriptors
A property descriptor is the metadata JavaScript uses to define how an object property behaves. A data descriptor stores a value and writable flag; an accessor descriptor stores get and set functions. Both kinds have enumerable and configurable attributes. Descriptors power reflection, controlled APIs, class semantics, and built-in object behavior.
Data and accessor properties
const record = {};
Object.defineProperty(record, "id", {
value: 42,
writable: false,
enumerable: true,
configurable: false,
});
With Object.defineProperty, omitted boolean flags default to false—unlike properties written in an object literal, which are usually writable, enumerable, and configurable. writable:false prevents changing a data value; configurable:false prevents deleting or reconfiguring it; enumerable:false excludes it from many enumeration APIs. A descriptor cannot be both data and accessor (for example, it cannot contain both value and get).
Object.getOwnPropertyDescriptor inspects an own property. Object.defineProperties defines multiple properties, and Reflect.defineProperty reports success as a Boolean instead of throwing in some failure cases. Proxies can intercept descriptor operations but must obey invariants about non-configurable properties.
Freezing and practical use
Object.freeze makes own properties non-configurable and data properties non-writable, but it is shallow: nested objects remain mutable. Descriptor-heavy APIs can make state harder to understand, so use them for deliberate contracts rather than routine object creation. Test enumeration and serialization because descriptor flags affect both.
Interview questions
- What are the two descriptor kinds? Data descriptors (
value,writable) and accessor descriptors (get,set). - What are the common flags?
writable,enumerable, andconfigurable(plus value/get/set). - What defaults apply in defineProperty? Omitted Boolean attributes default to false.
- Is freeze deep? No, it affects only the object’s own properties.