Object.definePropertyadvanced// 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.
When configurable: false, the property cannot be deleted and its descriptor cannot be changed (except loosening writable from true to false).
Challenge: Implement lockProperty(obj, key, val) that defines a non-configurable, non-writable property.
const o = {}
lockProperty(o, 'id', 1)
delete o.id // silently fails
o.id // β 1