A JavaScript object is composed of properties which may fall into two categories—data properties and accessor properties. A property is identified by a name and described by four attributes. The attributes
used to describe a property are typically enclosed into a so called descriptor object.
- Attributes describing data properties are value, writable,
configurable, and enumerable. - Attributes describing accessor properties are getter, setter,
configurable, and enumerable.
From the figure above, we can see that "configurabe" is shared by both categories.This article addresses some issues about this attribute and its subtle relation to "writable" in terms of data properties.
Typically, "configurable" constraints configurations we can perform on the underlying property, that is, whether or not can we change the attribute values. On the other hand, it also controls whether a property can be deleted. If
a property is not configurable, you can‘t change its configurable or enumerable attributes and you can‘t delete the property. Since the "configurable" is shared, our disscussion divides into two parts:
I. data property
If the "configurable" attribute of a data property is false, you are prohibited from doing the following:
- changing the "writable" attribute from false to true, but you can change it from true to false. (Enforcing restrictions of altering property value is acceptable.)
- changing it to an accessor property.
Q: If a property is not writable, is its value locked down from any change?
A: It depends. If writable is false, you cannot alter the property value by assigning to object_name.property_name. However, if
configrable is true, you can always change the property value by calling Object.defineProperty() to configure the value attribute to the new one, even though assigning to object_name.property_name is
forbidden when writable is false. In a word, to lock down a property value, you have to make configurable and writable false at the same time.
II. accessor property
If the "configurable" attribute of an accessor property is false, you are prohibited from doing the following:
- changing the getter or setter method.
- changing the underlying property to a data property
The above relections are personal thoughts based on 《JavaScript_The Definite Guide_Six-edition》.
Reflections on "configurable" Attribute (JavaScript)