B
Bryan Ray
I am trying to write an inheritance function, so I can call a base
classes method that has been overridden in the derived class. I want to
get rid of the ugly 'call()' syntax that would be used. Ideally I would
like to call the method using syntax such as:
object.base.method()
The idea is to use an object ('base') to provide the base classes
properties and proxy methods to the real methods stored in '_class' -
see the function below. The problem is that I can't think how to get at
the 'object's this reference from the 'base' (above), which is required
by 'apply()'.
I would have to store a reference to the object in each 'base', that
would then have to be specific to the object, not in the prototype. This
has a memory overhead proportional to the number of objects
instantiated, which I would like to avoid.
Has anyone done this before? Any thoughts?
Bryan
This is what I have so far (slimmed down a little):
// Inheritance function
function inherit(base,derived)
{
derived.prototype = new base();
derived.prototype.base = new base();
derived.prototype.base._class = new base();
// Required because the constructor may add methods.
var b = new base();
for( var i in b )
{
if(typeof(b) == "function")
{
// This is where the problem is...
//this.base._class['"+i+"'].apply(this,arguements)
}
}
}
classes method that has been overridden in the derived class. I want to
get rid of the ugly 'call()' syntax that would be used. Ideally I would
like to call the method using syntax such as:
object.base.method()
The idea is to use an object ('base') to provide the base classes
properties and proxy methods to the real methods stored in '_class' -
see the function below. The problem is that I can't think how to get at
the 'object's this reference from the 'base' (above), which is required
by 'apply()'.
I would have to store a reference to the object in each 'base', that
would then have to be specific to the object, not in the prototype. This
has a memory overhead proportional to the number of objects
instantiated, which I would like to avoid.
Has anyone done this before? Any thoughts?
Bryan
This is what I have so far (slimmed down a little):
// Inheritance function
function inherit(base,derived)
{
derived.prototype = new base();
derived.prototype.base = new base();
derived.prototype.base._class = new base();
// Required because the constructor may add methods.
var b = new base();
for( var i in b )
{
if(typeof(b) == "function")
{
// This is where the problem is...
//this.base._class['"+i+"'].apply(this,arguements)
}
}
}