JS Built-ins
ObjectObject.getOwnPropertyDescriptorintermediate

Object.getOwnPropertyDescriptor() β€” getter descriptor

πŸ“– Usage Example
// Inspect a single property's descriptor
const obj = { x: 42 }
Object.getOwnPropertyDescriptor(obj, 'x')
// β†’ { value: 42, writable: true, enumerable: true, configurable: true }

Use Object.getOwnPropertyDescriptor() to retrieve the descriptor object that describes a property's attributes.

Object.getOwnPropertyDescriptor() β€” accessor descriptor

For properties defined with a getter, the descriptor contains a get function instead of value and writable.

Challenge: Implement isGetter(obj, key) that returns true if the descriptor for key has a get function.

const o = {}
Object.defineProperty(o, 'x', { get: () => 1 })
isGetter(o, 'x') // β†’ true
isGetter(o, 'y') // β†’ false
πŸ’‘ Hints (2)
  • β€’ Accessor descriptors have `get` and/or `set`. Data descriptors have `value` and `writable`.
  • β€’ Use `typeof desc?.get === "function"` to check.