JS: Object.setPrototypeOf

By Xah Lee. Date: . Last updated: .

New in JS2015.

Object.setPrototypeOf(obj, parent)
Set the parent of obj to parent. Return the changed obj.
const aa = {};

// create a child object bb whose parent is aa
const bb = Object.create(aa);

// bb's parent is aa
console.log(Reflect.getPrototypeOf(bb) === aa);

// now change parent
const cc = Object.setPrototypeOf(bb, Array);

// return value is the new bb
console.log(cc === bb);
console.log(Reflect.getPrototypeOf(bb) === Array);
// no error, if arg is not object
const zz = Object.setPrototypeOf(2, Array);

JavaScript, Get Set Prototype

BUY ΣJS JavaScript in Depth