JS Built-ins
← Object111 / 150
ObjectObject.setPrototypeOfintermediate

Object.setPrototypeOf() β€” basics

πŸ“– 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()

Object.setPrototypeOf(obj, proto) sets the prototype (i.e., the internal [[Prototype]] property) of a specified object to another object or null.

Challenge: Implement setProto(obj, proto) that sets the prototype of obj to proto.

πŸ’‘ Hints (1)
  • β€’ setPrototypeOf modifies the prototype chain at runtime