accessing overridden methods

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)
}
}
}
 
L

Lasse Reichstein Nielsen

Bryan Ray said:
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() ....
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?

Thoughts: Give up.

You want one object to, somehow, know another object, that it happens
to be a property of. There is no way to do that from the inner object
alone. Remember, one object can be a property of many other objects at
the same time. The access path, "object.base.method()", only remembers
one level, the object of the method for use as the method's "this"
value. The remainder of the path is forgotten, so when you call
"method", all it knows is that it belongs to "base", and "base" is just
an object - it doesn't know who has a reference to it.

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top