I\'m new to Javascript programming and I\'m approaching my first application (a game, indeed) from an object oriented programming perspective (I know js is not really object
In OOP, when you're are defining a constructor of a sub class you are also (implicitly or explicitly) choosing a constructor from the super type. When a sub object is constructed both constructor are executed, first that from the super class, the all the other down the hierarchy.
In javascript this must be explicitly called and don't come automatically!
function Thing() {
this.relatedThings = [];
}
Thing.prototype.relateThing = function(what){
this.relatedThings.push(what);
}
function ThingA(){
Thing.call(this);
}
ThingA.prototype = new Thing();
function ThingA1(){
ThingA.call(this);
}
ThingA1.prototype = new ThingA();
function ThingA2(){
ThingA.call(this);
}
ThingA2.prototype = new ThingA();
If you don't this way, all the instance of ThingA, ThingA1 and ThingA2 shere the same relatedThings array constructed in the Thing constructor and called once for all instance at line:
ThingA.prototype = new Thing();
Globally, infact, in your code, you have only 2 calls to the Thing constructor and this result in only 2 instances of the relatedThings array.