i've got some problems with javascript prototype inheritance when this inheritance is stretched between multiple objects: surely i'm doing something wrong but for the moment i'm failing to understand what.
The whole Prototype's inheritance system seems to lose available methods when extending a prototype that already extends a prototype.
An example:
consider the following Objects
- object A
- prototype with extended function ab
- prototype with extended function cd
- object B
- extends A
- object C
- extends B
- prototype with extended function ef
- object D
- extends C
here an example of these Objects, as i have defined them:
Object A
function A () {
this.someproperty = someValue;
}
A.prototype.ab = function () {
// does something
}
A.prototype.cd = function () {
// does something
}
Object B
function B () {
A.call(this);
this.someOtherProperty = someValue;
}
B.prototype = A.prototype;
B.prototype.constructor = B;
Object C
function C () {
B.call(this);
}
C.prototype = B.prototype;
C.prototype.constructor = C;
C.prototype.ef = function () {
// does something
}
Object D
function D () {
C.call(this);
this.someOtherProperty = someValue;
}
D.prototype = C.prototype;
D.prototype.constructor = D;
given the example above, i am expecting that initializing a variable as "new D", such variable should have available the methods ab, cd, and ef, accessible with
- variable.ab()
- variable.cd()
- variable.ef()
It seems instead that all of these are undefined.
Please consider that if i initialize "new B" instead:
- variable.ab()
- variable.cd()
are defined and working
am i doing something wrong or prototype inheritance cannot be over stretched over multiple objects?
Thank you!
new D()? For exampleObject.keys(new D())will give you thesomePropertyandsomeOtherPropertyas these belong to the instance, but the methods do not and will not be listed.console.log((new D()).ab)should still give you a function reference though.