Property Descriptors
A property descriptor is the set of hidden settings attached to every object property: its value, whether it can be changed, whether it shows up in loops, and whether it can be redefined or deleted.
Detailed Explanation
A property is not only a name and a value. Each property also carries three or four flags that decide how it behaves. Those flags together are the property descriptor.
There are two kinds. A data descriptor has value and writable. An accessor descriptor has get and set functions instead. Both kinds also have enumerable and configurable. A property is one kind or the other, never both, so you cannot give a property a value and a get at the same time.
What each flag means:
| Flag | Effect when false |
|---|---|
writable | Assignment to the property is ignored, or throws in strict mode |
enumerable | Hidden from for...in, Object.keys, and JSON.stringify |
configurable | The property cannot be deleted and its flags cannot be changed |
The reason to care is that the defaults differ depending on how the property was created, and that difference bites people. Write obj.id = 7 or use a literal, and all three flags are true. Create the same property with Object.defineProperty(obj, "id", { value: 7 }) and all three default to false: read-only, invisible to Object.keys, and permanently locked. Nothing warns you. Object.defineProperty is the low-level operation, and low-level means nothing is assumed on your behalf.
Object.getOwnPropertyDescriptor(obj, key) shows you the truth for one property, and Object.getOwnPropertyDescriptors(obj) for all of them. That second function exists because copying an object with spread or Object.assign reads values through getters and writes plain data properties, which flattens accessors into frozen snapshots. Object.create(proto, Object.getOwnPropertyDescriptors(source)) is the faithful copy.
Descriptors also explain built-in behaviour that otherwise looks like magic. Class methods are non-enumerable, which is why Object.keys(instance) never lists them and why they do not leak into for...in. Class fields, by contrast, are enumerable. Object.freeze works by setting writable: false and configurable: false on every own property. Array.prototype.length is a writable but non-enumerable, non-configurable property, which is why arr.length = 0 truncates an array.
Use descriptors when you need a property that behaves differently from an ordinary one: a constant that must not be reassigned, an internal cache key you do not want serialised into JSON, or a computed property defined after construction. Use them sparingly otherwise.
The costs are readability and rigidity. A defineProperty call is harder to read than an assignment, and a reader who does not know the defaults will misjudge it. Non-configurable is genuinely permanent, so a mistake cannot be undone for the lifetime of that object. Non-writable properties also fail silently in sloppy mode, which turns a bug into a mystery. Because module code and class bodies are always strict, the same mistake throws there instead, which is usually preferable.
Code Examples
const config = {};
// Ordinary assignment: writable, enumerable, configurable all true.
config.retries = 3;
console.log(Object.getOwnPropertyDescriptor(config, "retries"));
// => { value: 3, writable: true, enumerable: true, configurable: true }
// defineProperty: every omitted flag defaults to false.
Object.defineProperty(config, "apiVersion", { value: "v2" });
console.log(Object.getOwnPropertyDescriptor(config, "apiVersion"));
// => { value: 'v2', writable: false, enumerable: false, configurable: false }
console.log(Object.keys(config)); // => [ 'retries' ] (apiVersion hidden)
console.log(JSON.stringify(config)); // => {"retries":3}
console.log(config.apiVersion); // => v2 (readable, only not writable)
// A locked property cannot be deleted or redefined either.
delete config.apiVersion;
console.log(config.apiVersion); // => v2"use strict"; // in a module or class body this is already the case
const account = { balance: 100 };
// Read-only but still visible to Object.keys and JSON.
Object.defineProperty(account, "currency", {
value: "INR",
enumerable: true,
writable: false,
configurable: false,
});
try {
account.currency = "USD";
} catch (err) {
console.log(err.constructor.name); // => TypeError
// Without strict mode this assignment would be ignored with no error at all.
}
// Spread reads a getter once and stores the result; descriptors do not survive.
const source = {
first: "Ada",
last: "Lovelace",
get full() {
return `${this.first} ${this.last}`;
},
};
const flattened = { ...source };
const faithful = Object.create(
Object.getPrototypeOf(source),
Object.getOwnPropertyDescriptors(source), // keeps `full` as a getter
);
source.first = "Grace";
console.log(flattened.full); // => Ada Lovelace (frozen snapshot)
console.log(faithful.full); // => Grace Lovelace (still computed)Common Gotchas
`defineProperty` defaults to `false`, assignment defaults to `true`
the two ways of creating a property produce opposite flags. If you use `Object.defineProperty` and want normal behaviour, you must spell out `writable`, `enumerable`, and `configurable` explicitly every time.
Redefining a non-configurable property throws
a second `Object.defineProperty` call on a locked property raises `TypeError: Cannot redefine property`. The single exception is flipping `writable` from `true` to `false`, which is allowed because it only ever removes capability.
Writing to a read-only property fails quietly in sloppy mode
the assignment is discarded and no error is raised, so the next read returns the old value. Inside ES modules and class bodies, strict mode is automatic and you get a `TypeError` instead.
Spread and `Object.assign` lose descriptors
both copy values, not metadata, so getters become plain values and non-enumerable properties are skipped entirely. Use `Object.getOwnPropertyDescriptors` when the metadata matters.