Object.setPrototypeOf(obj, proto)
made it into ES6.The section 15.2.3.2 speaks clearly:
15.2.3.2 Object.setPrototypeOf ( O, proto )Without going into details, the most basic polyfill woud be like this:
When thesetPrototypeOf
function is called with arguments O and proto, the following steps are taken:
- If Type(O) is not Object, then throw a TypeError exception.
- If Type(proto) is neither Object or Null, then throw a TypeError exception.
- Let status be the result of calling the [[SetInheritance]] internal method of O with argument proto.
- ReturnIfAbrupt(status).
- If status is false, then throw a TypeError exception.
- Return O.
(function(O,s){ O[s]||(O[s]=function(o,p){o.__proto__=p;return o}) }(Object,'setPrototypeOf'));If you want to go into details, the full reliable polyfill is hard to write down due all inconsistencies across JS engines out there where the __proto__ setter cannot be reused which means it's not possible to trust this magic over objects created from null.
All you need to do is forget __proto__ and use the suggested polyfill until the day __proto__ can simply disappear from those specs pages.
Enjoy!
This is a full polyfill with some extra info you might want to analyze, whenever ployfill is strictly true or false.