Hi -
I'm trying to figure out how to access an object's superclass object when using prototype inheritance - for example, it would be very helpful for a method to be able call the superclass method it overrides.
Accessing the superclass is no problem with a class hierarchy of just 2 levels, but gets very confusing if the hierarchy is > 2 levels. Problem is all objects in the hierarchy see the very top base class of the hierarchy as their superclass. So class3 level erroneously sees class1 level as its superclass, not class2 level. All the workaround ideas I've tried seem either still call the top base class or end up with an infinite loop where the the call to the superclass ends up calling itself.
Any help or suggestions would be greatly appreciated!!
I've included a small code test to illustrate various unsuccessful attempts to solve this problem - basically the code is trying to use multiple classes to add additional style attributes, etc. to a block element :
<html>
<head>
<SCRIPT LANGUAGE="JavaScript1.2" TYPE="text/javascript">
<!--
/******************************************/
function makeBlock() {
alert( "GOT2 makeBlock");
var b = document.createElement("DIV");
b.innerHTML = "JUNK";
b.id = "defaultId";
return b;
}
/**************************************************/
function c1(init) {
alert("GOT2 c1");
if(init) {
alert("c1 init " +arguments[0] );
// all subclasses of BasicBlock inherit these as public methods.
c1.prototype.initBlk = function() { this.blk = makeBlock(); };
c1.prototype.insert = function() {
with (this) document.body.appendChild(blk);
};
return null;
}
this.blk = null;
alert("Calling initBlk from c1");
this.initBlk();
this.name = "defaultName";
}
/******************************************/
function c2(init) {
alert("GOT2 c2");
if(init) {
alert("c2 init " +arguments[0] );
//this.superClass = c1;
c2.prototype = new c1("c2");
//c2.prototype.superClass = c1;
return null;
}
alert("calling c2 superClass");
//void this.superClass();
this.blk.style.color = "red";
}
/******************************************/
function c3(init) {
alert("GOT2 c3");
if(init) {
alert("c3 init " +arguments[0] );
c3.prototype = new c2("c3");
//c3.prototype.superClass = c2;
this.superClass = c2;
return null;
}
alert("calling c3 superClass");
void this.superClass();
this.blk.style.fontSize = "3.0em";
}
/*********************************************/
function test() {
void new c3("init");
var b1 = new c3();
b1.insert();
}
//-->
</SCRIPT>
</head>
<body onload="test();">
</body>
</html>
I'm trying to figure out how to access an object's superclass object when using prototype inheritance - for example, it would be very helpful for a method to be able call the superclass method it overrides.
Accessing the superclass is no problem with a class hierarchy of just 2 levels, but gets very confusing if the hierarchy is > 2 levels. Problem is all objects in the hierarchy see the very top base class of the hierarchy as their superclass. So class3 level erroneously sees class1 level as its superclass, not class2 level. All the workaround ideas I've tried seem either still call the top base class or end up with an infinite loop where the the call to the superclass ends up calling itself.
Any help or suggestions would be greatly appreciated!!
I've included a small code test to illustrate various unsuccessful attempts to solve this problem - basically the code is trying to use multiple classes to add additional style attributes, etc. to a block element :
<html>
<head>
<SCRIPT LANGUAGE="JavaScript1.2" TYPE="text/javascript">
<!--
/******************************************/
function makeBlock() {
alert( "GOT2 makeBlock");
var b = document.createElement("DIV");
b.innerHTML = "JUNK";
b.id = "defaultId";
return b;
}
/**************************************************/
function c1(init) {
alert("GOT2 c1");
if(init) {
alert("c1 init " +arguments[0] );
// all subclasses of BasicBlock inherit these as public methods.
c1.prototype.initBlk = function() { this.blk = makeBlock(); };
c1.prototype.insert = function() {
with (this) document.body.appendChild(blk);
};
return null;
}
this.blk = null;
alert("Calling initBlk from c1");
this.initBlk();
this.name = "defaultName";
}
/******************************************/
function c2(init) {
alert("GOT2 c2");
if(init) {
alert("c2 init " +arguments[0] );
//this.superClass = c1;
c2.prototype = new c1("c2");
//c2.prototype.superClass = c1;
return null;
}
alert("calling c2 superClass");
//void this.superClass();
this.blk.style.color = "red";
}
/******************************************/
function c3(init) {
alert("GOT2 c3");
if(init) {
alert("c3 init " +arguments[0] );
c3.prototype = new c2("c3");
//c3.prototype.superClass = c2;
this.superClass = c2;
return null;
}
alert("calling c3 superClass");
void this.superClass();
this.blk.style.fontSize = "3.0em";
}
/*********************************************/
function test() {
void new c3("init");
var b1 = new c3();
b1.insert();
}
//-->
</SCRIPT>
</head>
<body onload="test();">
</body>
</html>