Reputation: 43
In my understanding, if configurable setted to false, the property descriptor cannot be changed (re-define not allowed).
But when I test in chrome, node, firefox, safari, I get same results:
o = {}
Object.defineProperty(o, 't', {writable: true, configurable: false})
Object.defineProperty(o, 't', {writable: false, configurable: false}) // descriptor changes without error
Object.defineProperty(o, 't', {writable: true, configurable: false})
// this time I got expected error - TypeError: Attempting to change writable attribute of unconfigurable property.
It seems like when writable is true, even configurable is false, we still could change the property descriptor.
Or, change a descriptor to false is always allowed?
What is the right behavior?
Upvotes: 2
Views: 1858
Reputation: 371069
What configurable
does is:
true if and only if the type of this property descriptor may be changed and if the property may be deleted from the corresponding object.
And see:
Property descriptors present in objects come in two main flavors: data descriptors and accessor descriptors. A data descriptor is a property that has a value, which may or may not be writable. An accessor descriptor is a property described by a getter-setter pair of functions. A descriptor must be one of these two flavors; it cannot be both.
The type of a property descriptor refers to whether the descriptor is a data descriptor (one for which a value is meaningful) or an accessor descriptor (with a getter/setter). A non-configurable property cannot be changed from a data type to a getter/setter type, or vice versa.
const o = {};
Object.defineProperty(o, 't', {writable: true, configurable: false})
console.log(Object.getOwnPropertyDescriptor(o, 't'));
Object.defineProperty(o, 't', { get() { }})
If you pass a writable
key, the descriptor type defaults to a data descriptor, whose value is undefined
if no value
is passed.
The error you're seeing at the end is different. While you can change a writable non-configurable data descriptor to a non-writable data descriptor, you can't go the other way around - a non-writable, non-configurable data descriptor cannot be converted to a writable data descriptor. (See step 7 of ValidateAndApplyPropertyDescriptor)
Upvotes: 4