Object.definePropertyintermediate// 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.
Setting writable: false makes a property read-only. In non-strict mode, assigning to it silently fails; in strict mode it throws a TypeError.
Challenge: Implement makeReadOnly(obj, key, val) that defines a non-writable property.
const o = {}
makeReadOnly(o, 'x', 1)
o.x = 99 // silently fails in non-strict
o.x // β 1