By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,402 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,402 IT Pros & Developers. It's quick & easy.

Inheritace and super.class calling

P: n/a
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

Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Da Costa Gomez <dc*@xs4all.nl> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
> 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.

Jul 20 '05 #3

P: n/a
Douglas Crockford wrote:
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.


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.

Jul 20 '05 #4

P: n/a
> 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

Jul 20 '05 #5

P: n/a
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
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.