Da Costa Gomez wrote:
Hi,
I was wondering whether someone could shed some light on the following.
Using inheritance in Java one can override a function f() (or is it
overload?) in the child and then do:
public f() {
super.f();
...
}
in the child to first execute the parent stuff to be followed by the
additional child stuff.
Is there a way to accomplish this in JS as well and if so how?
I've already figured out the normal inheritance bit and complete
function overriding. I'm just looking for the above case.
TIA,
Fermin DCG
After studying both suggestions I came up with the following solution (&
1 nagging question).
Solution:
function ClassA () {
}
ClassA.prototype.baseFunction = function() {
return ("ClassA::baseFunction() called");
};
ClassA.prototype.deriveFunction = function() {
return ("ClassA::deriveFunction() called");
};
function ClassB () {
A = new ClassA();
this.extend(A); // Takes care of 'copying' the whole ClassA structure
// Override function code
_deriveFunction = A.deriveFunction;
this.deriveFunction = function() {
return("ClassB::deriveFunction() called with " + _deriveFunction());
}
}
Doing this allows me to keep ClassA as it is and ClassB carries the
deriveFunction functionality of A *plus* the added functionality of itself.
The question that is left:
Using
ClassB.prototype.deriveFunction = function() {
return ("ClassB::deriveFunction() called"+_deriveFunction());
};
instead of the this.deriveFunction IN ClassB will MOT get the desired
result. Instead it will always stay equal to the ClassA version.
Does it have something to do with the public nature of the ClassA
definition?
Thx for your input.
Cheers,
Fermin DCG