Re: How do I call a base class member function that has been overriden?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Lasse Reichstein Nielsen (lrn_at_hotpop.com)
Date: 02/12/04


Date: Thu, 12 Feb 2004 21:32:15 +0100


"Vince C." <noname@hotmail.com> writes:

> "Lasse Reichstein Nielsen" <lrn@hotpop.com> a écrit dans le message de
> news:fzdhp3q1.fsf@hotpop.com...
>> "Vince C." <none@hotmail.com> writes:
>>
>> > function classA()
>> > {
>> > this.doSmth = function () {
>> > ... // Do something
>> > }
>> > }

Let's start here. Why not:
 function classA(){}
 classA.prototype.doSmth = function(){ ... };
This only creates one function, instead of creating a new one every
time classA is instantiated.

>> > function classB()
>> > {
>> > this.inheritsFrom = classA;
>> > this.inheritsFrom();

The effect of these two lines (except from storing the classA function as
a property) can also be achieved by
         classA.call(this);

>> > this.doSmth = function() {
>> > ... // Do something else but:
>> > // Call base classA.doSmth()

but this still overwriets the change, since it all happens on the new
object which has only classB's empty prototype object as prototype.

> Thanks. But can you tell me how I can achieve what I'd like? Or if I'm
> mistaken, i.e. want to do something the wrong way, would you mind suggesting
> a smart way, please?

You still have the problem of not having A's doSmth around at all. It
was overwritten by the second assignment to doSmth. In order to have
a prototype object at all, you should assign it to classB's prototype
property:

 function classB() { ... }
 classA.call(classB.prototype);

This will make the classA function assign its doSmth function to
classB's *prototype*. Makeing a new instance of classB will then
inherit it, before creating its own.

I don't think there is an easy way to access the prototype's
property. The "hackish" way would be:

function () { // B's doSmth
 // ...
 var myself = this.doSmth;
 delete this.doSmth;
 this.doSmth(); // prototype's doSmth
 this.doSmth = myself;
 // ...
}

There is no safe way to access the prototype object without a
reference to it, and there is no way to bypass the property on the
"topmost" object, except to remove it (as above).

In recent browsers, you can use the
 this.hasOwnProperty("doSmth")
to test whether the new object has its own doSmth property, or whether
it uses the one from the prototype (in which case you should not delete
it).

/L

-- 
Lasse Reichstein Nielsen  -  lrn@hotpop.com
 DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
  'Faith without judgement merely degrades the spirit divine.'


Relevant Pages

  • Re: Question about constructor property vs. instanceof
    ... Pray call C.prototype.foohere without referring ... to the constructor or the prototype object ... Anyhow, now try that in a prototype method -- it will not work, because ...
    (comp.lang.javascript)
  • Re: Attach a class method to event handler
    ... Now the method of the prototype object will be called ... reference to the prototype object there are clearly not the same thing, ... reference to a Function object that calls this prototype method (as ...
    (comp.lang.javascript)
  • Re: simply super
    ... instead of from the prototype object of that constructor), ... next revision of the ECMAScript Specification if the proposed feature is ... Norris.prototype.kick and the prototype chain would work its way. ... functionality added in the Norris.prototype.kick method. ...
    (comp.lang.javascript)
  • Re: prototype object and arrays - Im confused
    ... prototype object works.. ... var A = function ... Can someone explain me why JS doesn't create array copy for every new ... return TRUE for 'r' while doing it with prototype will return FALSE. ...
    (comp.lang.javascript)
  • Re: need help with casting operators...
    ... > implemented the copy constructor with the other class as a parameter... ... > operator to convert ClassB to ClassA will be invoked. ... However if the prototype is myfunc, ...
    (comp.lang.cpp)