JS: Reflect.setPrototypeOf
(new in ECMAScript 2015)
Reflect.setPrototypeOf(obj, parent)-
Sets the parent of obj to parent. return true if success.
- If obj is not a object throw a TypeError exception.
- If parent is not a object nor
nullthrow a TypeError exception.
const aa = {}; const bb = Object.create(aa); // bb's parent is aa console.assert(Reflect.getPrototypeOf(bb) === aa); // now change parent let cc = Reflect.setPrototypeOf(bb, Array); // return value is true console.assert(cc); // verify parent console.assert(Reflect.getPrototypeOf(bb) === Array);
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.