JavaScript: Object.setPrototypeOf

By Xah Lee. Date: .

New in ES2015.

Object.setPrototypeOf ( obj, proto )

Set the parent of obj to proto.

let a = {};
let b = Object.create (a);

// b's parent is a
console.log ( Object.getPrototypeOf ( b ) === a ); // true

// now change parent
let cc = Object.setPrototypeOf ( b, Array );

// returns b itself, with the new parent
console.log ( cc === b ); // true

// b has new parent
console.log ( Object.getPrototypeOf ( b ) === Array ); // true

〔►see JavaScript: Prototype and Inheritance

〔►see JavaScript: Find Object's Prototype

Reference

ECMAScript® 2016 Language Specification#sec-object.setprototypeof

Object and Inheritance Topic

  1. JavaScript: Object System Overview
  2. JavaScript: What's Object?
  3. JavaScript: Prototype and Inheritance
  4. JavaScript: Create Object
  5. JavaScript: Object Literal Expression
  6. JavaScript: Find Object's Prototype
  7. JavaScript: How to Create Object with Parent X?
  8. JavaScript: Prevent Adding Property
  9. JavaScript: Determine Type of Object
  10. JavaScript: Primitive Value Object Wrapper
  11. JavaScript: Clone, Deep Copy Object/Array
  12. JavaScript: Test Equality of Objects

  1. JavaScript: Object Object
  2. JavaScript: Object.prototype
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.