accessing overridden methods

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bryan Ray

    accessing overridden methods

    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.met hod()

    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,de rived)
    {
    derived.prototy pe = new base();
    derived.prototy pe.base = new base();
    derived.prototy pe.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._clas s['"+i+"'].apply(this,arg uements)
    }
    }
    }
  • Lasse Reichstein Nielsen

    #2
    Re: accessing overridden methods

    Bryan Ray <bryan@nospam.c om> writes:
    [color=blue]
    > 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.met hod()[/color]
    ....[color=blue]
    > 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?[/color]

    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.me thod()", 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 - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    Working...