Can someone tell me, please, for what reason this is allowed (?):
function A()
{
A.prototype.getA = function()
{
return "A!";
}
}
function B()
{
B.prototype.getB = function()
{
return "B!";
}
}
// Purposely placed here.
B.prototype = new A();
function SuperA()
{
SuperA.prototype.getSuperA = function()
{
return "SUPER A!";
}
}
A.prototype = new SuperA();
var objectB = new B();
alert(objectB.getA()); // A!
alert(objectB.getB()); // B!
alert(objectB.getSuperA()); // Error...
For what reason is permissible change a heritage structure, even in use? This change in inheritance structure should not be reflected?
Here is a possible / alleged inheritance among 3 types. Of course, the right thing to do would be to change the position of the statement "B.prototype = new A();", but I left that way on purpose.
That's because you can't declare the inheritance structure. All inheritance is done dynamically at runtime, so there is no point in time where it's no longer possible to change it.
When you use an instance of one class as a prottype for another, it's inheriting that instance, not just copying the structure. Example:
function A() {}
A.prototype = {
hello: function() {}
};
var a = new A();
function B() {}
B.prototype = a;
var b = new B();
// all instances of B has the hello method
b.hello();
// add a method to the A instance
a.goodbye = function() {};
// now all instances of B have that method
b.goodbye();