Inheritace and super.class calling

D

Da Costa Gomez

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
 
L

Lasse Reichstein Nielsen

Da Costa Gomez said:
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?

Not directly.

Javascript doesn't have classes. As such, there is no notion of a
super class. All you have are singular objects, and prototype
inheritance.

Assume we have an object with a method "bar" in its prototype.

In Javascript there is no simple way to access the prototype of an
object (like the "super" keyword in, e.g., Java). You can use
"Foo.prototype.bar" (if the constructor function used to create the
object was "Foo"), but it is not absolutely safe. Someone can have
changed the prototype property of Foo since the object was created.

o.bar = function() {alert("The answer is: "); Foo.prototype.bar();}

You can rename the old function before shadowing it:

o.oldBar = o.bar;
o.bar = function() {alert("The answer is: "); this.oldBar();}

You can even make it a local variable, only available to the new bar
function:

o.bar = (function(oldBar) {
return function() {alert("The answer is: "); oldBar.call(this);}
})(o.bar);
I've already figured out the normal inheritance bit and complete
function overriding. I'm just looking for the above case.

You could try looking at Smalltalk or Self. They are closer to
Javascript in Object methodology (as prototype based languages rather
than class based).

/L
 
D

Douglas Crockford

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.

Yes, check out http://www.crockford.com/javascript/inheritance.html

Since JavaScript is class-free, you have a lot more options than Java-like
classical inheritance. Usually, when you are looking to extend but super, what
you really want to do is augment.
 
D

Da Costa Gomez

Douglas said:
Yes, check out http://www.crockford.com/javascript/inheritance.html

Since JavaScript is class-free, you have a lot more options than Java-like
classical inheritance. Usually, when you are looking to extend but super, what
you really want to do is augment.

Read the piece and there is one thng that still eludes me though.
I'm using the following to extend classes:

Object.prototype.extend = function (oSuper) {
for (sProperty in oSuper) {
this[sProperty] = oSuper[sProperty];
}
}

This would take care of all the methods available in the parent to be
'send' to the child.

I am assuming that something like:

ChildClass.prototype.overriddenFunction = function {
// Execute this.uber("overriddenFunction");
// ... method particular stuff
return this;
}

Should basically seal it for me. Or am I completely wrong in the
assumption that *ChildClass.prototype.overriddenFunction* gets the job
done of overriding the parent version?

Concerning the uber function I can appreciate the apply bit but I just
do not see how one would seperate the uber function from the
encapsulation method function so that it can be 'executed' similar to
super().

Or am I way of and spinning around with a blind-fold on?

TIA
Fermin DCG



Creating properties (+ getters & setters) is done by calling 1 method only.
 
D

Douglas Crockford

Or am I way of and spinning around with a blind-fold on?

Maybe so. While it is possible to approximate the Java style of classes in
JavaScript, I don't think it is, ultimately, wise. Greater efficiencies come in
JavaScript with the augmentation patterns. These, I feel, more than compensate
for the lack of strong typing. If you really want to be writing in the classical
style, you would do better to stick with Java. JavaScript feels you from the
need to construct complex class hierarchies. Each object is just exactly what it
needs to be, with no class overhead.

http://www.crockford.com/javascript/little.html
 
D

Da Costa Gomez

Da said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top