JS: Reflect.setPrototypeOf

By Xah Lee. Date: . Last updated: .

(new in ECMAScript 2015)

Reflect.setPrototypeOf(obj, parent)

Sets the parent of obj to parent.

  • If obj is not a object throw a TypeError exception.
  • If parent is not a object nor null throw a TypeError exception.
const aa = {};
const bb = Object.create(aa);

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

// now change parent
let cc = Reflect.setPrototypeOf(bb, Array);

// return value is true
console.assert((cc) === true);

// verify parent
console.assert((Reflect.getPrototypeOf(bb) === Array) === true);

Reflect.setPrototypeOf vs Object.setPrototypeOf

Reflect.setPrototypeOf is a better version of Object.setPrototypeOf.

the reflect version errors out if argument is not an object.

JavaScript. Get Set Prototype