473,473 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Inheritance : access to the superClass' methods via prototype

Hi all,

What I am trying to achieve is an 'inherits' method similar to Douglas
Crockford's (http://www.crockford.com/javascript/inheritance.html) but
that can enable access to the superclass' priviledged methods also. Do
you know if this is possible ?

In the following example, I create an ObjectA (variable a), an ObjectB
which inherits ObjectA (variable b) and an ObjectC which inherits
ObjectA (variable c1). The 'toString ()' method of ObjectC refers to
the 'toString ()' method of ObjectA. This is possible because the
'Object.inherits ( superClass )' method adds a reference to the
superClass of an object in the object's prototype.

If I understood things correctly, the prototype is the same for all
objects of class ObjectC. Therefore, I should be able to only add the
reference to the superClass once. That is the purpose of the
'Object.initializedPrototypes' array : it keeps track of the objects
for which a reference to the superClass has been added to the
prototype.

However, when I create another instance of ObjectC (variable c2), its
prototype doesn't contain any reference to the superClass.

A workaround for this problem consists in adding a reference to the
superClass for each instance of the inferiting object (either by
bypassing the check to Object.initializedPrototypes or by adding the
reference to a priviledged member such as 'this.superClass' instead of
'this.prototype.superClass'). However, in terms of memory usage or of
"programming elegance", this seams to defeat the whole purpose of using
prototypes.

Here is the code, if any of you have got ideas they are more than
welcome !

Thanks,

Stephane.

PS : I also included the 'Object.isInstanceOf ( classOrSuperClass )'
method although it is not used in the examples. It's there just in case
you spot anything wrong with it. It seems to work but it might show
some flaws in my understanding of OOP in JavaScript.

<html>
<head>
<script language = "JavaScript"><!--

///////////////////////////////
///// Array
///////////////////////////////
Array.prototype.indexOf = function ( element ) {
for ( var i = 0; i < this.length; i ++ )
if ( this [ i ] == element ) return i;
return -1;
}

///////////////////////////////
///// Object
///////////////////////////////
// array of names of previously initialized objects.
// this enables the inherits method to not add the superClass for an
inheriting class twice.
Object.prototype.initializedPrototypes = new Array ();

Object.prototype.inherits = function ( superClass ) {
this.prototype = new superClass;
this.prototype.constructor = this;
if ( Object.initializedPrototypes.indexOf ( this.constructor.name )
== -1 ) {
// this is the first time the object calls 'inherits', I must therefore
add the superClass to the prototype.
// if I understood things correctly, this should add a "pointer" to
superClass for all instances of this object.
this.prototype.superClass = new superClass;
Object.initializedPrototypes.push ( this.constructor.name );
}
// the line below proves me wrong ! this.prototype.superClass is
undefined (when called for variable c2)
else alert ( this.constructor.name + ' should already have a
superClass : ' + this.prototype.superClass );
superClass.call ( this );
return this;
}

Object.prototype.isInstanceOf = function ( classOrSuperClass ) {
var ptr = this;
do {
if ( ptr.constructor == classOrSuperClass ) return true;
ptr = ptr.prototype.superClass;
} while ( ptr != null );
return false;
}

///////////////////////////////
///// Example
///////////////////////////////
function ObjectA () {
this.letter = 'A';
this.toString = function () {
return 'I am an instance of ObjectA.\nMy favorite letter is ' +
this.letter + '.\n';
};
}

function ObjectB () {
this.inherits ( ObjectA );
this.letter = 'B';
}

function ObjectC () {
this.inherits ( ObjectA );
this.letter = 'C';
// overriding toString method of ObjectA, using the equivalent of
super.toString
this.toString = function () {
return this.prototype.superClass.toString.call ( this ) + 'I am an
ObjectC.\n';
};
}

var a = new ObjectA (), b = new ObjectB (), c1 = new ObjectC ();

// the following line will generate an alert (see
Object.prototype.inherits method)
// because an ObjectC has already been instanciated.
var c2 = new ObjectC ();

// alerts which call the toString method of each object.
alert ( a );
alert ( b );
alert ( c1 );
alert ( c2 );

//--></script>
</head>
</html>

Sep 20 '05 #1
2 3009
Hi

No Javascript doesn't have a real "super" property.

I have made a simple example of how it's implymented here
http://km0ti0n.blunted.co.uk/viewng....28223185937500 .

By the looks of things it's how you have done it.

Sep 20 '05 #2
thanks !

although your solution wasn't exactly what I had intended, it pointed
me in the right direction.

The important line in your bit of code is :
subClass.prototype.FunctionA = function() {/*[...]*/};

Whereas my way of writing it was this :
this.prototype.FunctionA = function() {/*[...]*/};

This led me to test whether both this and subClass pointed to the same
thing, which they don't ! It is still a little mysterious to me why it
is this way, but my conclusion is that the object's prototype is the
common structure for all of its instances, but the actual instance can
change the prototype "locally" (i.e. without impacting the structure of
the other instances of the same class). Therefore, to enable your
solution in a generic function (i.e. without making an explicit
reference to a specific subClass), I changed :

this.prototype.superClass = /*[...]*/;

to :

this.constructor.prototype.superClass = /*[...]*/;

Below is the corrected code (for whomever it might be useful). Thanks
km0ti0n for your help (and thanks to Douglas Crockford's site on
javascript (http://www.crockford.com/javascript/) for putting me on the
right track),

Stephane.

PS : If some of you still spot errors in this code, please post to tell
me : I'm not yet sure this version is correct ...

///////////////////////////////
///// Array
///////////////////////////////
Array.prototype.indexOf = function ( element ) {
for ( var i = 0; i < this.length; i ++ )
if ( this [ i ] == element ) return i;
return -1;
}

///////////////////////////////
///// Object
///////////////////////////////
// array of names of previously initialized objects.
// this enables the inherits method to not add the superClass for an
inheriting class twice.
Object.prototype.initializedPrototypes = new Array ();

Object.prototype.inherits = function ( superClass ) {
this.prototype = new superClass;
this.prototype.constructor = this;
if ( Object.initializedPrototypes.indexOf ( this.constructor.name )
== -1 ) {
// this is the first time the object calls 'inherits', I must therefore
add the superClass to the prototype.
this.constructor.prototype.superClass = new superClass;
Object.initializedPrototypes.push ( this.constructor.name );
}
superClass.call ( this );
return this;
}

Object.prototype.isInstanceOf = function ( classOrSuperClass ) {
if ( classOrSuperClass == Object ) return true;
var ptr = this;
do {
if ( ptr.constructor == classOrSuperClass ) return true;
ptr = ptr.constructor.prototype.superClass;
} while ( ptr != null );
return false;
}

///////////////////////////////
///// Example
///////////////////////////////
function ObjectA () {
this.letter = 'A';
this.toString = function () {
return 'I am an instance of ObjectA.\nMy favorite letter is ' +
this.letter + '.\n';
};
}

function ObjectB () {
this.inherits ( ObjectA );
this.letter = 'B';
}

function ObjectC () {
this.inherits ( ObjectA );
this.letter = 'C';
// overriding toString method of ObjectA, using the equivalent of
super.toString
this.toString = function () {
return this.constructor.prototype.superClass.toString.cal l ( this
) + 'I am an ObjectC.\n';
};
}

var a = new ObjectA (), b = new ObjectB (), c1 = new ObjectC (), c2
= new ObjectC ();

// alerts which call the toString method of each object.
alert ( a );
alert ( b );
alert ( c1 );
alert ( c2 );

Sep 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
5
by: Robert Spoons | last post by:
Can you look over this code, preferably try it, and comment? I believe the 'extend' function below will allow you to use full 'class inheritance' in javascript, but I would like to verify it. ...
2
by: Kevin Newman | last post by:
I have been playing around with a couple of ways to add inheritance to a JavaScript singleton pattern. As far as I'm aware, using an anonymous constructor to create a singleton does not allow any...
36
by: Pacific Fox | last post by:
Hi all, haven't posted to this group before, but got an issue I can't work out... and hoping to get some help here ;-) I've got a base object that works fine with named arguments when called...
6
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance"...
4
by: amidzic.branko | last post by:
I'm trying to solve a problem using inheritance and polymorphism in python 2.4.2 I think it's easier to explain the problem using simple example: class shortList:
11
by: John | last post by:
Hi All, Although C# has Generics, it still does not support the generic programming paradigm. Multiple inheritance is required to support real generic programming. Here is a simple design pattern...
1
by: ITMozart | last post by:
I wrote this piece of code: class A { public A() { someMeth(); } public void someMeth() { out.println(entries); }
6
by: howa | last post by:
Consider example: Animal = function(age) { this.age = age; }; Animal.prototype.sleep = function() { alert("Animal Sleeping..."); };
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.