Difficulty: Advanced
What are property descriptors in JavaScript? How does Object.defineProperty work, and what do writable, enumerable and configurable control?
Every property on a JavaScript object has a hidden descriptor that controls how it behaves: its value or getter/setter, and three boolean flags, writable, enumerable and configurable. Object.defineProperty(obj, key, descriptor) creates or changes a property with an exact descriptor, and Object.getOwnPropertyDescriptor(obj, key) reads one. This is how you make read-only, hidden or computed properties, and it is the machinery under Object.freeze, class getters and Vue 2's reactivity.
A descriptor is one of two kinds, never both:
value and writable, plus enumerable and configurable.get and/or set functions, plus enumerable and configurable.Mixing them, for example { value: 1, get() {} }, throws a TypeError.
| Flag | When false | Default via assignment | Default via defineProperty |
|---|---|---|---|
writable | Value cannot be reassigned (silent fail in sloppy mode, TypeError in strict mode) | true | false |
enumerable | Hidden from for...in, Object.keys, JSON.stringify, spread | true | false |
configurable | Cannot be deleted, cannot switch between data and accessor, flags cannot be changed (except writable true to false) | true | false |
The defaults column is the classic trap: Object.defineProperty(obj, 'x', { value: 1 }) creates a property that is read-only, hidden and permanent, because every omitted flag defaults to false.
Object.preventExtensions(obj): no new properties can be added.Object.seal(obj): preventExtensions plus every property becomes configurable: false. Values can still change.Object.freeze(obj): seal plus every data property becomes writable: false.Object.defineProperties and Object.getOwnPropertyDescriptors work on many properties at once. Object.create(proto, Object.getOwnPropertyDescriptors(src)) is a way to copy an object including its getters, which spread and Object.assign cannot do (they invoke getters and copy the values).All three object helpers are shallow: freezing a config object does not freeze its nested objects.
Array.prototype.map is non-enumerable, which is why for...in over an array does not list methods. Math.PI is non-writable and non-configurable.defineProperty. That is why it could not detect newly added keys, and why Vue 3 moved to Proxy.TypeError there.configurable is false, you cannot make it configurable again.'use strict';
const payment = { orderId: 'ord_9F2', amountInPaise: 49900 };
Object.defineProperty(payment, 'gatewaySecret', {
value: 'sk_test_123',
// writable, enumerable, configurable all default to false
});
console.log(Object.keys(payment));
console.log(JSON.stringify(payment));
console.log(Object.getOwnPropertyDescriptor(payment, 'gatewaySecret'));
try {
payment.gatewaySecret = 'changed';
} catch (e) {
console.log(e.constructor.name);
}
try {
delete payment.gatewaySecret;
} catch (e) {
console.log(e.constructor.name);
}
Omitting the flags makes the property hidden from keys and JSON, read-only and undeletable. In strict mode both the write and the delete throw instead of failing silently.
const cart = { items: [{ price: 300 }, { price: 450 }] };
Object.defineProperty(cart, 'total', {
get() { return this.items.reduce((sum, i) => sum + i.price, 0); },
enumerable: true,
});
console.log(cart.total);
cart.items.push({ price: 250 });
console.log(cart.total); // recomputed on every read
const config = Object.freeze({ api: { version: 'v1' } });
config.api.version = 'v2'; // nested object is not frozen
console.log(config.api.version, Object.isFrozen(config), Object.isFrozen(config.api));
A getter computes the value on every access, so total stays in sync with items. Object.freeze only freezes the top level, so nested objects need a recursive deep freeze.
Property Descriptor, Object.defineProperty, writable, enumerable, configurable, Accessor Property