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

Inheritance : access to the superClass' methods via prototype

P: n/a
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
Share this Question
Share on Google+
2 Replies


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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.