Puoi farti un oggetto che gestica le liste.
Ti do' un esempio (da completare)
codice:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<script language="JavaScript" type="text/javascript">
<!--
function List() {
this.elements = new Array();
this.ind = -1;
this.curSep = ";";
this.type = 'list';
//
this.addElement = _addElement;
//this.removeElement = _removeElement;
this.push = _push;
this.pop = _pop;
this.asString = _asString;
this.separator = _separator;
this.listLength = _length;
this.reset = _reset;
this.next = _next;
this.setList = _setList;
} // function List()
function _addElement(anElement,position) {
if (position == null) {
this.push(anElement);
}
} // function _addElement(anElement)
function _reset() {
this.ind = -1;
} // function reset()
function _next() {
this.ind++;
if (this.ind > this.listLength-1) {
return null;
} // if (this.ind >= this.length)
return this.elements[this.ind];
} // function next()
function _push(anElement) {
this.elements.push(anElement);
} // function push(anElement)
function _pop(anElement) {
return this.elements.pop();
} // function push(anElement)
function _asString() {
return this.elements.toString();
} // function _asString()
function _separator(aSep) {
this.curSep = aSep;
} // function _separator(aSep)
function _length() {
return this.elements.length;
} // function _length()
function _setList(aString,aSep) {
if (aSep == null) {
sep = this.curSep;
} else {
sep = aSep;
} // if (aSep == null)
this.curSep = sep;
this.elements = aString.split(sep);
} // function _setList(aString,aSep)
//-->
</script>
<body>
<script language="JavaScript" type="text/javascript">
<!--
a = new List();
a.setList("a;b;c;d;e;f;g;h;i");
a.reset();
b = a.next();
while (b != null) {
document.write(b);
b = a.next();
}
document.write("
");
document.write(a.asString());
document.write("
");
//
a.push('k');
document.write("
");
document.write(a.asString());
document.write("
");
while (a.listLength() != 0) {
a.pop();
document.write(a.asString());
document.write("
");
}
//-->
</script>
</body>
</html>