Skip to content
Back to blog

Introduction to Common JS APIs

An introduction to Object.defineProperty's property descriptors, their default values, and the mutual exclusion between accessor descriptors and data descriptors.

Updated

Object.defineProperty

In JS objects, you can define a descriptor for each object field, which can be understood as controlling the behavior of the object property, such as readable, writable, enumerable, configurable, and so on.

The descriptor fields supported by a property are as follows:

Descriptor field Function Default value
configurable Whether the descriptor fields (including itself) can be modified false
enumerable Whether this field can be enumerated in iterators
If set to false, it cannot be picked up by Object.keys, Object.values, for ... in, Object.assign, ..., and so on
false
value The default value of the field undefined
writable Whether the property value can be modified
When set to false, assigning to the property throws in strict mode
false
get The field's getter, triggered when the value is read undefined
set The field's setter, triggered when the value is set undefined

set + get cannot coexist with value + writable; their functionality overlaps