function Entity() {
this.a = {a: 4};
this.b = 5;
}
function Thing() {}
Thing.prototype = new Entity;
var thing1 = new Thing;
thing1.a.a = 3;
thing1.b = 4;
var thing2 = new Thing;
console.log(thing2.a.a); // prints 3 instead of 4. I would like 4.
console.log(thing2.b) // prints 5 as wanted
I'm having difficulty with setting prototypical inheritance in javascript. Ideally I want both thing1 and thing2 to have their own clean copy of the "new Entity prototype".
using this.__proto__ is something I want to avoid
[Edit]
I have a rough idea of how this working.
Setting thing1.b sets the b property on the Thing instance. which does not touch the Entity.b defined in the prototype chain.
Where as setting thing1.a.a on the Thing instance cannot been done because it would throw an "cannot set a on undefined" error. That's when it goes up the prototype chain to find Entity.a which is defined and sets Entity.a.a to a new value.
[Further Edit]
As @IvoWetzel says setting thing1.b does not touch the prototype chain because setting properties does not. Where as setting thing1.a.a does two steps. A getter on thing1.a which touches the prototype chain followed by a setter of .a