Property Descriptors and Object.defineProperty
Property descriptors control attributes such as writable, enumerable, and configurable for data properties. Accessor properties use get and set instead of a direct value.
Object.defineProperty exposes this lower-level object model and is useful for libraries, polyfills, and controlled APIs.
Non-Writable Property
const config = {};
Object.defineProperty(config, 'version', {
value: '1.0.0',
writable: false,
enumerable: true,
configurable: false
});
console.log(config.version);💡 The descriptor creates a property whose value cannot be reassigned through ordinary assignment.
Key Takeaways
- Property descriptors control property behavior.
- Data descriptors and accessor descriptors have different fields.
- defineProperty enables precise object API design.
Knowledge Check
Which descriptor controls whether a data property's value can be changed by assignment?