PDA

View Full Version : How do I declare the array I need?


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 + "&nbsp; &nbsp; ";
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'";

beetle
09-26-2002, 10:12 PM
Just like for objects, arrays, strings, etc, constructor functions require use of the new keyword. Stick those in and see what happens

anItem.anchor = new anAnchor();
anItem.anchored = new isAnchored();
etc...

adios
09-27-2002, 05:06 AM
Not completely sure what's up but I'll take a stab at it: looks like you're creating a custom JS class of link objects of some sort. This is typically done with a constructor function in place of a class definition; the constructor organizes the object instances around a central point for more cohesive programming. JS also supports an interesting method of inheritance: assigning methods (and common properties) to the prototype property (object) of the constructor function makes the properties 'appear' in all instances, even previously created ones (the runtime engine checks the constructor's Prototype object if it fails to find the property in the instance itself). This can dramatically save memory and time. Anyway...

Just guessing at some of your intent. You populate arrays in JS by populating them...no shorthand that I'm aware of.

http://devedge.netscape.com/viewsource/2001/oop-javascript/
http://www.dansteinman.com/dynduo/en/newobjects.html
http://www.webmasterbase.com/article/470
http://www.webreference.com/js/column79/