Skip to content

Property Descriptors

Every property of an object carries a few settings besides its value, which together are called its property descriptor. They decide whether the value can be changed, whether the property shows up when the object’s keys are listed, and whether the property can be deleted or reconfigured. Properties created in the usual way have every setting switched on. Object.defineProperty creates or changes a property with the settings you choose, which is how libraries make read-only or hidden properties.

Object.getOwnPropertyDescriptor(obj, name) returns the descriptor of one property:

const book = { title: 'Moby-Dick', year: 1851 };
console.log(Object.getOwnPropertyDescriptor(book, 'title'));

This prints:

{
value: 'Moby-Dick',
writable: true,
enumerable: true,
configurable: true
}
Setting When true Default with defineProperty
writable the value can be changed by assignment false
enumerable the property is listed by for...in, Object.keys, spread and JSON.stringify false
configurable the property can be deleted, and its settings can be changed false

Object.defineProperty(obj, name, descriptor) adds a property or changes an existing one. Any setting left out of the descriptor of a new property defaults to false, so a property defined with only a value is read-only, hidden and permanent:

const book = { title: 'Moby-Dick', year: 1851 };
Object.defineProperty(book, 'catalogueId', {
value: 'B-2291',
});
console.log(book.catalogueId);
console.log(Object.getOwnPropertyDescriptor(book, 'catalogueId'));
console.log(Object.keys(book));
console.log(JSON.stringify(book));
book.catalogueId = 'changed';
delete book.catalogueId;
console.log(book.catalogueId);

This prints:

B-2291
{
value: 'B-2291',
writable: false,
enumerable: false,
configurable: false
}
[ 'title', 'year' ]
{"title":"Moby-Dick","year":1851}
B-2291

The assignment and the delete both fail silently here, because the example runs as a classic script in sloppy mode. In strict mode, which applies in ES modules and classes, each of them throws a TypeError.

When defineProperty is called on a property that already exists, settings left out of the descriptor keep their current values, so you can change one setting at a time:

const book = { title: 'Moby-Dick', year: 1851 };
Object.defineProperty(book, 'title', { writable: false });
book.title = 'Typee';
console.log(book.title);
Object.defineProperty(book, 'year', { enumerable: false });
for (const key in book) {
console.log(key);
}
console.log(book.propertyIsEnumerable('year'));
console.log(Object.getOwnPropertyNames(book));

This prints:

Moby-Dick
title
false
[ 'title', 'year' ]

A non-enumerable property is still there and can still be read as book.year. It is just left out of listings. Object.getOwnPropertyNames lists it anyway, because it returns every own property with a string key, enumerable or not.

A property that is not configurable cannot be deleted, and its settings are locked, with one exception: writable can still be changed from true to false, though not back. Its value can still change if it is writable:

const book = { title: 'Moby-Dick' };
Object.defineProperty(book, 'edition', {
value: 1,
writable: true,
enumerable: true,
configurable: false,
});
book.edition = 2;
console.log(book.edition);
try {
Object.defineProperty(book, 'edition', { enumerable: false });
} catch (err) {
console.log(err.message);
}
delete book.edition;
console.log(book.edition);

This prints:

2
Cannot redefine property: edition
2

defineProperty throws even in sloppy mode, while the delete fails silently.

A descriptor describes either a data property, with value and writable, or an accessor property, with get and set functions. Both kinds have enumerable and configurable. This adds a getter and setter to an existing object, in the same way as get and set in an object literal, which Getters & Setters covers:

const order = { net: 40 };
Object.defineProperty(order, 'gross', {
get() {
return this.net * 1.2;
},
set(value) {
this.net = value / 1.2;
},
enumerable: true,
configurable: true,
});
console.log(order.gross);
order.gross = 60;
console.log(order.net);
console.log(Object.getOwnPropertyDescriptor(order, 'gross'));

This prints:

48
50
{
get: [Function: get],
set: [Function: set],
enumerable: true,
configurable: true
}

Write get and set as ordinary functions or method shorthand, not arrow functions, because an arrow function has no this of its own and would not see the object. Mixing the two kinds in one descriptor is an error:

const book = { title: 'Moby-Dick' };
try {
Object.defineProperty(book, 'x', { value: 1, get() { return 1; } });
} catch (err) {
console.log(err.message);
}

This prints:

Invalid property descriptor. Cannot both specify accessors and a value or writable attribute, #<Object>

Object.defineProperties takes an object of descriptors keyed by property name, and Object.getOwnPropertyDescriptors returns them all:

const book = {};
Object.defineProperties(book, {
title: { value: 'Middlemarch', enumerable: true },
author: { value: 'George Eliot', enumerable: true },
shelf: { value: 'B4' },
});
console.log(Object.keys(book));
console.log(Object.getOwnPropertyDescriptors(book));

This prints:

[ 'title', 'author' ]
{
title: {
value: 'Middlemarch',
writable: false,
enumerable: true,
configurable: false
},
author: {
value: 'George Eliot',
writable: false,
enumerable: true,
configurable: false
},
shelf: {
value: 'B4',
writable: false,
enumerable: false,
configurable: false
}
}

Object.getOwnPropertyDescriptors is also the way to copy an object together with its getters and setters, as Object.defineProperties({}, Object.getOwnPropertyDescriptors(source)), because spread and Object.assign copy only the values the getters return.

Descriptors work property by property. To lock a whole object at once, see Seal, Freeze and PreventExtensions.

Steve Griffith covers this topic in his video JavaScript Object Property Descriptors.