Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no Object.setPrototypeOf(...) in ECMAScript standard?

Apparently using __proto__ property is still the main way of manipulating prototype chains, even though this is not standards compliant and IE does not support it. Though you can also construct inheritance through the use of new constructor this seems like an unnecessary complication compared to __proto__ property or standards compliant Object.getPrototypeOf function.

Edit:

As stated in the answers, this method does exist now (ES6 standard). Be aware of the performance warning, though: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf

like image 470
JussiR Avatar asked Jul 01 '12 12:07

JussiR


2 Answers

It is part of the ES6 harmony draft:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf

I am using it now in the latest release of Chrome.

var proto = {
    foo: 'bar'
};

var object = {};

Object.setPrototypeOf(object, proto);

console.assert(object.foo == 'bar');
like image 54
kzh Avatar answered Oct 19 '22 21:10

kzh


Brendan Eich says this here:

Object.setPrototypeOf is not going to happen. Writable __proto__ is a giant pain to implement (must serialize to cycle-check) and it creates all sorts of type-confusion hazards. You may think you want it as a low-level sharp instrument. JS is not that language. Higher-level forms for classes and mixins seem much better and do not involve such sharp edges.

like image 39
Esailija Avatar answered Oct 19 '22 22:10

Esailija