Sub topic 12
Property Descriptors: writable, enumerable, configurable
This chapter assumes you can create objects, add and delete properties, and list them with Object.keys and for...in. It also assumes you know that properties can be inherited through the prototype chain.
Every property has a value and three hidden flags, called attributes. Together they make up the property's descriptor, which controls whether the property can be changed, listed, or removed.
The Three Attributes
| Attribute | When `false` | Default for literals and assignment | Default for `defineProperty` |
|---|---|---|---|
| `writable` | Assigning a new value fails | `true` | `false` |
| `enumerable` | `Object.keys`, `for...in`, spread, and `JSON.stringify` skip the property | `true` | `false` |
| `configurable` | You cannot delete the property or change its attributes | `true` | `false` |
A property with these attributes plus a value is a data property. An accessor property swaps value and writable for get and set; the next chapter covers it.
Inspecting and Defining Descriptors
Object.getOwnPropertyDescriptor reads the descriptor of one own property. Object.defineProperty creates a property or changes one that already exists.
const user = { name: "Ada" };
console.log(Object.getOwnPropertyDescriptor(user, "name"));
// { value: 'Ada', writable: true, enumerable: true, configurable: true }
Object.defineProperty(user, "id", { value: 42 });
console.log(Object.getOwnPropertyDescriptor(user, "id"));
// { value: 42, writable: false, enumerable: false, configurable: false }
console.log(Object.keys(user)); // [ 'name' ]
console.log(user.id); // 42The id property exists and you can read it. Because every attribute you leave out defaults to false, it is also read-only, hidden from listing, and permanent. This is the most common surprise with defineProperty. To see every own descriptor at once, use Object.getOwnPropertyDescriptors(obj).
writable: Silent Failure vs TypeError
Writing to a non-writable property never changes it. What else happens depends on the mode. Sloppy mode ignores the write without telling you. Strict mode, which includes ES modules and class bodies, throws a TypeError.
"use strict";
const account = {};
Object.defineProperty(account, "balance", {
value: 100,
writable: false,
enumerable: true,
configurable: true
});
try {
account.balance = 0;
} catch (e) {
console.log(e instanceof TypeError); // true
}
console.log(account.balance); // 100enumerable: Hidden, Not Private
A non-enumerable property is only left out of listings. Anyone can still read it by name, and Object.getOwnPropertyNames and Reflect.ownKeys still return it. Built-in methods such as Array.prototype.map are non-enumerable, which is why for...in over an array doesn't list them.
const item = { title: "Book" };
Object.defineProperty(item, "secret", { value: "x", enumerable: false });
console.log(JSON.stringify(item)); // {"title":"Book"}
console.log({ ...item }); // { title: 'Book' }
console.log(Object.getOwnPropertyNames(item)); // [ 'title', 'secret' ]configurable: The One-Way Door
Once configurable is false, delete fails (it throws in strict mode) and most attribute changes throw a TypeError. There are two exceptions. You can still change writable from true to false. You can still change the value if the property is writable.
"use strict";
const settings = {};
Object.defineProperty(settings, "mode", { value: "dark", writable: true });
Object.defineProperty(settings, "mode", { writable: false }); // allowed
try {
Object.defineProperty(settings, "mode", { enumerable: true });
} catch (e) {
console.log(e.name); // TypeError
}Common Errors
- Calling
definePropertyto "just add a property" and forgetting that the attributes you leave out becomefalse. - Treating a non-enumerable property as private data.
- Expecting an error in sloppy-mode code when a write fails without one.
- Setting
configurable: falsetoo early, so you can't change your mind later.
Exercise
You run Object.defineProperty(cfg, "port", { value: 8080, enumerable: true }) and then cfg.port = 9000 in sloppy mode. What is cfg.port, and can you later make the property writable?
Show answer
cfg.port is still 8080. Leaving out writable makes it false, so the sloppy-mode write is ignored. You can't make it writable later because configurable also defaulted to false, and a non-configurable property can't change writable from false to true.
Exercise
Name two ways to list a non-enumerable own string key that Object.keys misses.
Show answer
Object.getOwnPropertyNames(obj) and Reflect.ownKeys(obj). Reflect.ownKeys also returns symbol keys.