JS Built-ins
ObjectObject.definePropertyintermediate

Object.defineProperty() β€” non-writable property

πŸ“– Usage Example
// 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() β€” writable: false

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
πŸ’‘ Hints (2)
  • β€’ Set `writable: false` in the property descriptor.
  • β€’ In sloppy mode, assigning to a non-writable property is silently ignored.