Likewise, you can provide a setter without a getter, though this is a much less com‐
mon pattern.
Object Property Attributes
At this point, we have a lot of experience with object properties. We know that they
have a key (which can be a string or a symbol) and a value (which can be of any type).
We also know that you cannot guarantee the order of properties in an object (the way
you can in an array or
Map
). We know two ways of accessing object properties (mem‐
ber access with dot notation, and computed member access with square brackets).
Lastly, we know three ways to create properties in the object literal notation (regular
properties with keys that are identifiers, computed property names allowing noniden‐
tifiers and symbols, and method shorthand).
There’s more to know about properties, however. In particular, properties have
attributes that control how the properties behave in the context of the object they
belong to. Let’s start by creating a property using one of the techniques we know; then
we’ll use
Object.getOwnPropertyDescriptor
to examine its attributes:
const
obj
=
{
foo
:
"bar"
};
Object
.
getOwnPropertyDescriptor
(
obj
,
'foo'
);
This will return the following:
{
value
:
"bar"
,
writable
:
true
,
enumerable
:
true
,
configurable
:
true
}
The terms property attributes, property descriptor, and property con‐
figuration are used interchangeably; they all refer to the same thing.
This exposes the three attributes of a property:
Writable
Controls whether the value of the property can be changed.
Enumerable
Controls whether the property will be included when the properties of the object
are enumerated (with
for...in
,
Object.keys
, or the spread operator).
Configurable
Controls whether the property can be deleted from the object or have its
attributes modified.
Object Property Attributes | 303