S
stephane
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>
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>