Re: How do I call a base class member function that has been overriden?
From: Lasse Reichstein Nielsen (lrn_at_hotpop.com)
Date: 02/12/04
- Next message: Saint Jude: "Re: change text color of a link with onclick event"
- Previous message: Rick Koch: "Using DOM from HTC"
- In reply to: Vince C.: "Re: How do I call a base class member function that has been overriden?"
- Next in thread: Vince C.: "Re: How do I call a base class member function that has been overriden?"
- Reply: Vince C.: "Re: How do I call a base class member function that has been overriden?"
- Reply: Vince C.: "Re: How do I call a base class member function that has been overriden?"
- Messages sorted by: [ date ] [ thread ]
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.'
- Next message: Saint Jude: "Re: change text color of a link with onclick event"
- Previous message: Rick Koch: "Using DOM from HTC"
- In reply to: Vince C.: "Re: How do I call a base class member function that has been overriden?"
- Next in thread: Vince C.: "Re: How do I call a base class member function that has been overriden?"
- Reply: Vince C.: "Re: How do I call a base class member function that has been overriden?"
- Reply: Vince C.: "Re: How do I call a base class member function that has been overriden?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|