JS Built-ins
ObjectObject.getOwnPropertyNamesbeginner

Object.getOwnPropertyNames() β€” inherited not included

πŸ“– Usage Example
// Get all own property names, including non-enumerable
const obj = { a: 1 }
Object.defineProperty(obj, 'hidden', { value: 2, enumerable: false })
Object.getOwnPropertyNames(obj)   // β†’ ['a', 'hidden']

Use Object.getOwnPropertyNames() to list all own properties including non-enumerable ones, unlike Object.keys().

Object.getOwnPropertyNames() β€” own only

Inherited properties are NOT returned by Object.getOwnPropertyNames, even though they appear in for...in and with the in operator.

Challenge: Implement ownNamesOnly(obj) that returns the own property names and confirms they exclude inherited ones.

const proto = { inherited: 1 }
const obj = Object.create(proto)
obj.own = 2
ownNamesOnly(obj) // β†’ ['own']
πŸ’‘ Hints (1)
  • β€’ `Object.getOwnPropertyNames` does not traverse the prototype chain.