rossmason
05-29-2005, 03:51 AM
I am having problems with object orientated JavaScript and I am ready to tear my damn hair out!!!
I currently work for a web development company and we use JavaScript and content editable div’s for our web content management system. Quite frankly the code for this is pretty messy and I saw Object Oriented JavaScript maybe as our saviour to move some of the core code common to different components in the system to an extra level where it wouldn’t be ‘hacked’ on a regular basis.
The problem is that when a class is inherited I need to be able to get access to the parent classes variables and methods from within the child class with minimum code in the client class or I will have a hard time convincing the other developers to use the parent classes.
The test code I am using to try to get this to work is below. As you can see from the debug alerts I cannot access the parent class variables from inside the child class but outside the child class this doesn't seem to be a problem.
My main concearn is keeping the code in the child classes minimal. Any help anyone can give will be greatfully recieved.
<html>
<head>
<script language="javascript" type="text/javascript">
function initialise(){
ManagedElements = new Array
docElements = document.all
objTotal = docElements.length
/*Search through each body element and import any IScript objects into the ManagedElements array
as a javascript class of the name set by the attribute IType.
*/
for (objCount=0; objCount < objTotal; objCount++){
if (docElements[objCount].getAttribute("IScript") == "true") {
alert("Found IScriptElement: " + docElements[objCount].id + ". importing as class: " + docElements[objCount].getAttribute("IType"))
// Try and create a Class of type found in IType and return an error if Class cannot be found
try{
ManagedElements[ManagedElements.length] = (eval("new " + docElements[objCount].getAttribute("IType")))
ManagedElements[ManagedElements.length -1].initialise(docElements[objCount])
}
catch(error){
alert("Debug: Could not find class '" + docElements[objCount].getAttribute("IType") + "'. Import of iScript Element '" + docElements[objCount].id + "' failed.")
}
}
}
for (i=0; i<ManagedElements.length; i++){
alert(ManagedElements[i].id)
}
}
/*-----------------------------------------------------------
------------------ CLASS IComponent -------------------------
------------------ ------------------------------------------*/
function IComponent(){
this.initialise = IComponent_initialise
}
function IComponent_initialise(htmlObj){
this.htmlObject = htmlObj
this.id = htmlObj.id
this.type = htmlObj.getAttribute("IType")
}
/*-----------------------------------------------------------*/
TextEditor1.prototype = new IComponent();
TextEditor1.prototype.constructor = IComponent;
TextEditor1.superclass = IComponent.prototype;
function TextEditor1(){
alert("Creating IComponent. IType = TextEditor1")
alert(this.id)
}
TextEditor2.prototype = new IComponent();
TextEditor2.prototype.constructor = IComponent;
TextEditor2.superclass = IComponent.prototype;
function TextEditor2(){
alert("Creating IComponent. IType = TextEditor2")
alert(this.id)
}
</script>
</head>
<body onload="javascript:initialise()">
<div id="div1" IScript="true" IType="TextEditor1" style="border:1px dashed #ff0000" contenteditable="true">retret</div>
<div id="div2" IScript="true" IType="TextEditor2" style="border:1px dashed #ff0000" contenteditable="true">retret</div>
</body>
</html>
I currently work for a web development company and we use JavaScript and content editable div’s for our web content management system. Quite frankly the code for this is pretty messy and I saw Object Oriented JavaScript maybe as our saviour to move some of the core code common to different components in the system to an extra level where it wouldn’t be ‘hacked’ on a regular basis.
The problem is that when a class is inherited I need to be able to get access to the parent classes variables and methods from within the child class with minimum code in the client class or I will have a hard time convincing the other developers to use the parent classes.
The test code I am using to try to get this to work is below. As you can see from the debug alerts I cannot access the parent class variables from inside the child class but outside the child class this doesn't seem to be a problem.
My main concearn is keeping the code in the child classes minimal. Any help anyone can give will be greatfully recieved.
<html>
<head>
<script language="javascript" type="text/javascript">
function initialise(){
ManagedElements = new Array
docElements = document.all
objTotal = docElements.length
/*Search through each body element and import any IScript objects into the ManagedElements array
as a javascript class of the name set by the attribute IType.
*/
for (objCount=0; objCount < objTotal; objCount++){
if (docElements[objCount].getAttribute("IScript") == "true") {
alert("Found IScriptElement: " + docElements[objCount].id + ". importing as class: " + docElements[objCount].getAttribute("IType"))
// Try and create a Class of type found in IType and return an error if Class cannot be found
try{
ManagedElements[ManagedElements.length] = (eval("new " + docElements[objCount].getAttribute("IType")))
ManagedElements[ManagedElements.length -1].initialise(docElements[objCount])
}
catch(error){
alert("Debug: Could not find class '" + docElements[objCount].getAttribute("IType") + "'. Import of iScript Element '" + docElements[objCount].id + "' failed.")
}
}
}
for (i=0; i<ManagedElements.length; i++){
alert(ManagedElements[i].id)
}
}
/*-----------------------------------------------------------
------------------ CLASS IComponent -------------------------
------------------ ------------------------------------------*/
function IComponent(){
this.initialise = IComponent_initialise
}
function IComponent_initialise(htmlObj){
this.htmlObject = htmlObj
this.id = htmlObj.id
this.type = htmlObj.getAttribute("IType")
}
/*-----------------------------------------------------------*/
TextEditor1.prototype = new IComponent();
TextEditor1.prototype.constructor = IComponent;
TextEditor1.superclass = IComponent.prototype;
function TextEditor1(){
alert("Creating IComponent. IType = TextEditor1")
alert(this.id)
}
TextEditor2.prototype = new IComponent();
TextEditor2.prototype.constructor = IComponent;
TextEditor2.superclass = IComponent.prototype;
function TextEditor2(){
alert("Creating IComponent. IType = TextEditor2")
alert(this.id)
}
</script>
</head>
<body onload="javascript:initialise()">
<div id="div1" IScript="true" IType="TextEditor1" style="border:1px dashed #ff0000" contenteditable="true">retret</div>
<div id="div2" IScript="true" IType="TextEditor2" style="border:1px dashed #ff0000" contenteditable="true">retret</div>
</body>
</html>