Enumerator
The for-in loop doesn't work in ASP JavaScript the way it is
"suppose" to. In Visual Basic, the for-in loop enumerates the
members of a collection.
For Each item In the collection document.write item
Next. Many JavaScript users mistakenly assume that the JavaScript for-in
loop does the same thing. It does not -- the JavaScript for-in
loop enumerates the members of a JavaScript object:
var obj = new Object();
obj.foo = 1;
obj.bar = 2;
// the following "for-in" writes
"foo = 1 bar = 2
for (member in obj) document.write(member + "=" +
obj[member] + " ");
To enumerate the items in a collection, use the Enumerator object:
var myEnum = new Enumerator(myCollection);
for (myEnum; !myEnum.atEnd() ; myEnum.moveNext() )
document.write(myEnum.item());
}
myCollection can be for example:
- Request.Cookies
- Request.Form
- Request.QueryString
- Request.ServerVariables
- etc...
Enumerator objects are considerably more flexible than for-each-in
loops.
Several enumerators can be declared at the same time for each
collection, enumerators can be passed around as data, they can be reset
to the beginning at any time, etc.
Description
Provides a way to enumerate items in a collection.
Syntax
new Enumerator(collection)
The collection argument is any collection object.
Remarks
Collections differ from arrays in that the members of a collection are
not directly accessible. Instead of using indices, as you would with
arrays, you can only move the current item pointer to the first or
next element of a collection.
The Enumerator object provides a way to access any member of
a collection and behaves similarly to the For...Each statement
in VBscript, which repeats a group of statements for each element in
an array or collection.
For Each element In group
[statements]
[Exit For]
[statements]
Next [element]
- array
- A set of sequentially indexed elements having the same type of
data. Each element of an array has a unique identifying index
number. Changes made to one element of an array do not affect the
other elements.
-
- collection
- An object that contains a set of related objects. An object's
position in the collection can change whenever a change occurs in
the collection; therefore, the position of any specific object in
the collection may vary.
|