Make your own free website on Tripod.com

Some discussion about Inheritance


I will only discuss the syntax recommanded by Macromedia.

subClass=function(){
    super();
}
subClass.prototype=new superClass();


double constructor call ?

The superClass constructor get called when we establish the prototype object because we say "new superClass()". When a new instance is created by constructor, the super() called the superClass constructor again.

Lets see an example of superClass constructor and see what happens in the instance of subClass.

superClass=function(){
    this.x=5;
}
// Here I omit the script for subClass definition.
// see the script above. I dont repeat it here.

a1=new subClass();

Now a1.__proto__ has a property "x" created by "new superClass";

a1 also has a property "x" in local, that is created by super();

If we check debug-list variable, we will found that, all properties in local of a1 object can be found in its __proto__. Those properties in __proto__ are useless because they are masked by the same properties in local.

So, it is a waste of memory. However, the waste is in the prototype object. Even we create 100 instances, only the one copy in prototype object is a waste. So, it is not such a big deal.

Lets see another example of superClass constructor:

superClass=function(){
    _global.instanceCount++;
}
// See above script for definition of subClass.

a1=new subClass();

Before we create any instance, we create prototype by "new superClass()". Thus, the prorotype is in fact an instance of superClass object.

This will increase the "instanceCount" by one. If we create 3 instance of superClass and 6 instance of subClass, the instanceCount will be 10; (In fact, there are 4 instances of superClass not 3. The extra one is the subClass.prototype that we dont count.)


super() or not ?

Do "super()" is the rule. Omit "super()" is exception.

If the super constructor has been called when we creating prototype, all initiation should have been done.  Why we call the same super constructor again by super().  Are there any difference to initiate values to the __proto__ than to the local ?

In many cases, there is no difference. But not always.

Same function may create different results:

superClass=function(){
    this.init();
}
superClass.prototype.init=function(){this.x=5*this.ratio;}
superClass.prototype.ratio=10;

subClass=function(){
    super();
}
subClass.prototype=new superClass();
subClass.prototype.ratio=20;

obj1=new subClass();

When we see the debug, the first call of super constructor with "new" initiate x value to be 50 in the prototype. But the second call of super constructor by super() initiate x value to be 100 in the local.

That is an over-simplified example.

When we do subClassing, the prototype is born as an instance of superClass by new superClass(). What follows are block of scripts to modify or to over-drive the existing methods and properties. When we call super(), the super constructor will call these updated methods and properties. Then the values it initiated to the local will be different from what are initiated to the __proto__.

initiation of object or array

We have discussed this before (in the __proto__ session). Here is an example:

superClass=function(){
    this.styleTable=[];
}
subClass=function(){
    super();
}
subClass.prototype=new superClass();

obj1=new subClass();
obj1.styleTable.push(this);

For any instances of subClass, there is a styleTable in __proto__.  There is also a styleTable in local due to the implementation of super().

When obj1 push something to styleTable, it is pushed to the styleTable of its own.

If we omit super(), then there is no styleTable in loca. The data will be pushed to the styleTable of __proto__ which is shared by all instances. When another object push something to styleTable again, it will push to the shared styleTable and now the Array gets two elements.