JS Built-ins
← Object112 / 150
ObjectObject.setPrototypeOfintermediate

Object.setPrototypeOf() β€” set to null

πŸ“– 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() β€” null prototype

Setting the prototype to null creates an object with no prototype chain β€” similar to Object.create(null). Such objects have no inherited methods.

Challenge: Set an object's prototype to null and verify it loses inherited methods.

πŸ’‘ Hints (1)
  • β€’ null prototype objects are useful as pure hash maps with no inherited methods