LEHayes
09-26-2002, 06:48 PM
Below you'll find my program
Yes, I know I'm only supposed to post the code I'm having problems with, but this is my first program.
I'm trying to declare an array of objects which contain an array of objects. I can do this in pascal but I don't know how to do this in JavaScript; What I thought would work doesn't so what I want to ask is: How do I declare an array where each element is an object and an element of that object is an identical array?
I included the calling functions because I didn't get the expected result. I got a 'masterList' with a length of 0 in the 'display' function. That's how I know I'm not making the proper declaration.
var anItem = new Object();
anItem.prefix = "";
anItem._name = "";
anItem._target = "";
anItem._href = "";
anItem._onclick = "";
anItem.text = "";
anItem.suffix = "";
anItem.heading = false;
function anAnchor () {
return this.prefix + "<a" + this._name + this._target + this._href +
this._onclick + ">" + this.text + "</a>" + this.suffix;
}
anItem.anchor = anAnchor();
function isAnchored () {
if (this._name != "") return true;
if (this._href != "") return true;
if (this._onClick != "") {
this._href=" href=''";
return true;
}
return false;
}
anItem.anchored = isAnchored();
anItem.list = new Array();
function toggle (a,b) {
var list = a,
index = b;
if (index.length>1) {
toggle(list[index[0]].list, pop(index));
return false;
}
list[index[0]].heading = !list[index[0]].heading;
return false;
}
function display (a,b) {
var item = a,
indent = b,
cIndent = indent + "";
document.writeln("function display (a,b) - " + item.length);
for (i = 0 ; i < item.length ; i++) {
document.write(i + ": " + item.anchored);
if (item.anchored)
document.writeln(cIndent,item[i].anchor);
else
document.writeln(cIndent,item[i].text);
if (item[i].heading) {
cIndent = indent + " ";
display(item[i].list,cIndent);
cIndent = indent + "";
}
}
}
function displaySiteMap () {
document.write ("<table cellspacing='0' cellpadding='0' bgcolor='seashell'");
document.writeln(" width='100%'><tr><td align='center'>");
display(masterList," ");
document.writeln("</td></tr></table>");
}
masterList = new Array();
masterList[0].text = "<h3>Trouble's Brewing</h3>";
masterList[1].prefix = "Oerth Creation ";
masterList[1].text = "Myth";
masterList[1]._target = " _target='main'";
masterList[1]._href = " href='Oerth/creationMyth.html'";
Yes, I know I'm only supposed to post the code I'm having problems with, but this is my first program.
I'm trying to declare an array of objects which contain an array of objects. I can do this in pascal but I don't know how to do this in JavaScript; What I thought would work doesn't so what I want to ask is: How do I declare an array where each element is an object and an element of that object is an identical array?
I included the calling functions because I didn't get the expected result. I got a 'masterList' with a length of 0 in the 'display' function. That's how I know I'm not making the proper declaration.
var anItem = new Object();
anItem.prefix = "";
anItem._name = "";
anItem._target = "";
anItem._href = "";
anItem._onclick = "";
anItem.text = "";
anItem.suffix = "";
anItem.heading = false;
function anAnchor () {
return this.prefix + "<a" + this._name + this._target + this._href +
this._onclick + ">" + this.text + "</a>" + this.suffix;
}
anItem.anchor = anAnchor();
function isAnchored () {
if (this._name != "") return true;
if (this._href != "") return true;
if (this._onClick != "") {
this._href=" href=''";
return true;
}
return false;
}
anItem.anchored = isAnchored();
anItem.list = new Array();
function toggle (a,b) {
var list = a,
index = b;
if (index.length>1) {
toggle(list[index[0]].list, pop(index));
return false;
}
list[index[0]].heading = !list[index[0]].heading;
return false;
}
function display (a,b) {
var item = a,
indent = b,
cIndent = indent + "";
document.writeln("function display (a,b) - " + item.length);
for (i = 0 ; i < item.length ; i++) {
document.write(i + ": " + item.anchored);
if (item.anchored)
document.writeln(cIndent,item[i].anchor);
else
document.writeln(cIndent,item[i].text);
if (item[i].heading) {
cIndent = indent + " ";
display(item[i].list,cIndent);
cIndent = indent + "";
}
}
}
function displaySiteMap () {
document.write ("<table cellspacing='0' cellpadding='0' bgcolor='seashell'");
document.writeln(" width='100%'><tr><td align='center'>");
display(masterList," ");
document.writeln("</td></tr></table>");
}
masterList = new Array();
masterList[0].text = "<h3>Trouble's Brewing</h3>";
masterList[1].prefix = "Oerth Creation ";
masterList[1].text = "Myth";
masterList[1]._target = " _target='main'";
masterList[1]._href = " href='Oerth/creationMyth.html'";