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.
A non-enumerable property is hidden from Object.keys, for...in, and JSON.stringify, but can still be read directly.
Challenge: Implement addHidden(obj, key, val) that defines a non-enumerable property.
const o = {}
addHidden(o, '_id', 42)
o._id // β 42
Object.keys(o) // β [] (not visible)