By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,181 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

accessing overridden methods

P: n/a
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[i]) == "function")
{
// This is where the problem is...
//this.base._class['"+i+"'].apply(this,arguements)
}
}
}
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Bryan Ray <br***@nospam.com> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.