JS Built-ins
ObjectObject.createintermediate

Object.create() β€” inherited property access

πŸ“– Usage Example
// Create an object with a specific prototype
const proto = { greet() { return 'hi' } }
const child = Object.create(proto)
child.greet()   // β†’ 'hi'

Use Object.create() to create a new object with a specific prototype, enabling prototype-based inheritance.

Object.create() β€” reading inherited properties

Properties on the prototype are accessible via the created object even though they are not own properties. in finds them but Object.keys and hasOwnProperty do not.

Challenge: Implement canAccessInherited(proto, key) that creates an object with proto as its prototype and returns whether the key is accessible (using the in operator).

canAccessInherited({ role: 'admin' }, 'role') // β†’ true
canAccessInherited({ role: 'admin' }, 'age')  // β†’ false
πŸ’‘ Hints (2)
  • β€’ The `in` operator checks the prototype chain, unlike `Object.keys` or `hasOwnProperty`.
  • β€’ `key in obj` returns `true` for both own and inherited properties.