Say i have the following object types :
var Positionable = function() { };
Positionable.prototype.x = 0;
Positionable.prototype.y = 0;
var Sizable = function() { };
Sizable.prototype.w = 0;
Sizable.prototype.h = 0;
var Fadable = function() { };
Fadable.prototype.a = 0;
I would like to have a function allowing me to create a sub type that would inherit from several other types. For example, i could create a type Rectangle
that would inherit from both Positionnable
and Sizable
.
I could chain prototypes :
var Rectangle = function() { };
Sizable.prototype = new Positionable;
Rectangle.prototype = new Sizable;
But i don't like this method fo two reasons :
It causes Sizable to inherit from
Positionable
, which means that if i want another type to be Sizable without beingPositionable
, i can't do it;It implies that a
Sizable
object is alsoPositionnable
, which semantically isn't good since there is no reason for aSizable
object to bePositionnable
and not the other way.
So i first thought about merging prototypes, assuming i have a function void merge( type dst, type src, bool overwrite )
, in loop i would simply merge every base class prototype into the child type prototype. This would not create a chain (which is what i want as explained above)
But this causes another problem : since i'm not using new, the base constructors are not called. So i wonder, is there a way to also merge constructors ? For example, if i could access and reference the base constructors, i could store each of these base constructors in an array, and assign the child type constructor with a function calling each of these constructors consecutivly.
How could i do this with constructors ? Or maybe there is another way ?
Thanks for your help !