JS Built-ins
← Object114 / 150
ObjectObject.setPrototypeOfadvanced

Object.setPrototypeOf() β€” inheritance chain

πŸ“– Usage Example
// Set the prototype of an object
const animal = { breathe() { return true } }
const dog = {}
Object.setPrototypeOf(dog, animal)
dog.breathe()   // β†’ true

Use Object.setPrototypeOf() to dynamically change an object's prototype, though creating objects with the correct prototype upfront is preferred.

Object.setPrototypeOf() β€” building inheritance

Object.setPrototypeOf() can be used to set up an inheritance chain dynamically.

Challenge: Use Object.setPrototypeOf() to create a two-level inheritance chain.

πŸ’‘ Hints (1)
  • β€’ setPrototypeOf enables dynamic prototype chain manipulation