Object.definePropertybeginner// Define a property with a custom descriptor
const obj = {}
Object.defineProperty(obj, 'name', {
value: 'Alice',
writable: false,
enumerable: true,
})Use Object.defineProperty() to add or modify a property with precise control over its writability, enumerability, and configurability.
Object.defineProperty(obj, key, descriptor) defines a new property or modifies an existing one with precise control over its behaviour.
A data descriptor has value, writable, enumerable, and configurable.
Challenge: Implement addProperty(obj, key, val) that uses Object.defineProperty to add a writable, enumerable, configurable property.
const o = {}
addProperty(o, 'x', 42)
o.x // β 42