I use library supporting canvas drawing (easelJS) and i am still new to javascript. This library uses following 'class' hierarchy:
-DisplayObject
---Container
---Shape
---Bitmap
Now, all i want is to implement some additional functionality like simple physics engine. I want all of DisplayObjects (and descendants like container) to have it's own instances of additional properties, like for example velocity
When i try:
DisplayObject.prototype.velocity = new Vector2d();
It seems all instances share same velocity, even if later in my code I make assignmets like:
someShape.velocity.x = newValueX;
someShape.velocity.y = newValueY;
I know some solutions of this problem but its not satisfying for me:
I can make lots of classes like: MyShape (extending Shape), MyContainer(extending Container) etc, and in their constructur functions I can put
this.velocity = new Vector2d();
I can edit constructor of DisplayObject after downloading it, right in the library file (awful solution!)
I can remember to add this properties in runtime right after object creation (even more awful!)
someShape = new Shape() someShape.velocity = new Vector2d(); // 15 other properties here
Or i can add function to the prototype of DisplayObject
DisplayObject.prototype.exdendWithAdditionalProperties = new function(){ this.velocity = new Vector2d(); // 15 other properties here }
which i will still need to remember to call manually after object creation.
But it seems to lot of work to me and i believe javascript can provide better solution.
Please help!