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
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');
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With