Full answer
A property is more than a name and a value. The engine stores attributes beside it, and Object.getOwnPropertyDescriptor(obj, key) shows them.
A data property has value and three flags. writable decides whether assignment can change the value. enumerable decides whether the key appears in for...in, Object.keys, spread, and JSON.stringify. configurable decides whether the property can be deleted and whether its attributes can be changed again. Once configurable is false, it cannot be made true again.
An accessor property has get and set functions instead of value and writable. Reading runs the getter and writing runs the setter. Getters give computed or validated values that look like plain fields. Object literals and classes both support get and set syntax. A class getter lives on the prototype, so it is not an own property of the instance. As a result it is not copied by spread, and JSON.stringify ignores it.
Object.defineProperty creates or changes a property. Attributes not given default to false when it creates a property, but keep their current values when it changes an existing one. In contrast, properties created by normal assignment have all three flags set to true.
Object.freeze and Object.seal work by changing these flags on all properties. Spread and Object.assign read through getters and copy plain values, so the copy loses the accessor behavior.
'use strict';
const product = { name: 'Notebook' };
Object.defineProperty(product, 'sku', { value: 'NB-01' }); // all flags default to false
Object.defineProperty(product, 'internalNote', { value: 'reorder soon', enumerable: false, writable: true });
console.log(Object.getOwnPropertyDescriptor(product, 'name'));
console.log(Object.getOwnPropertyDescriptor(product, 'sku'));
console.log(Object.keys(product), JSON.stringify(product), product.internalNote);
try {
product.sku = 'CHANGED';
} catch (error) {
console.log('assign to read-only:', error.name);
}
try {
delete product.sku;
} catch (error) {
console.log('delete non-configurable:', error.name);
}
const temperature = {
celsius: 25,
get fahrenheit() {
return this.celsius * 9 / 5 + 32;
},
set fahrenheit(value) {
this.celsius = (value - 32) * 5 / 9;
},
};
console.log(temperature.fahrenheit);
temperature.fahrenheit = 212;
console.log(temperature.celsius);
console.log(typeof Object.getOwnPropertyDescriptor(temperature, 'fahrenheit').get);
const copy = { ...temperature };
console.log(Object.getOwnPropertyDescriptor(copy, 'fahrenheit').value, 'get' in Object.getOwnPropertyDescriptor(copy, 'fahrenheit'));
class Circle {
constructor(radius) {
this.radius = radius;
}
get area() {
return Math.round(Math.PI * this.radius ** 2);
}
}
const circle = new Circle(2);
console.log(circle.area, Object.keys(circle), JSON.stringify(circle), Object.hasOwn(circle, 'area'));Run with Node.js v24.22.1 as js-46.js; two runs gave identical output. Output: { value: 'Notebook', writable: true, enumerable: true, configurable: true } { value: 'NB-01', writable: false, enumerable: false, configurable: false } [ 'name' ] {"name":"Notebook"} reorder soon assign to read-only: TypeError delete non-configurable: TypeError 77 100 function 212 false 13 [ 'radius' ] {"radius":2} false
Key points
- Data attributes: value, writable, enumerable, configurable. Accessor attributes: get, set, enumerable, configurable.
- Properties made by assignment have every flag true; defineProperty defaults missing flags to false.
- Non-enumerable properties are hidden from keys, spread, and JSON.
- Non-configurable cannot be deleted or reconfigured, and cannot be switched back.
- Spread copies getter results as plain values.
Common mistakes
- Assuming defineProperty defaults flags to true; they default to false when creating.
- Expecting a class getter to appear in JSON.stringify or Object.keys output.
- Trying to make a non-configurable property configurable again.
- Forgetting that spread and Object.assign turn getters into plain values.
Follow-ups
- How does Object.freeze use these flags internally?
- How does a getter on the prototype differ from a getter defined directly on the instance?
- How can you define several properties at once and read all descriptors?
Tips
- List the four data flags, then say what each controls in one line.
- Show a getter and setter pair with a clear use, such as unit conversion.
- Mention that class getters live on the prototype; it answers the most common follow-up before it is asked.