Availability: |
| |||
JavaScript syntax: | IE | myEnumerator = Enumerator | ||
IE | myEnumerator = new Enumerator(aCollection) | |||
Argument list: | aCollection | The collection to be enumerated | ||
Object properties: | constructor | |||
Object methods: | atEnd(), item(), moveFirst(), moveNext() |
An Enumerator object provides a way to enumerate through all the objects in a collection (aka Array). You can create a new Enumerator, giving it your collection as an argument and can then access the items in the collection in a more sophisticated way than simply using a for loop.
You can use the enumerator to cycle through the items in a collection in much the same way as a for( ... in ... ) loop would enumerate the properties. However, in some collections, objects have more than one entry. They may have an indexed entry and an associative entry. An enumerator should traverse the collection visiting each item only once. A for loop may visit objects several times.
The available set of methods and properties is somewhat limited compared with enumerator objects in other languages.
Because this is only available on MSIE and is severely dysfunctional on the Macintosh version of MSIE 5, its use is somewhat limited from the portability point of view. It is recommended that you avoid using it for the time being. Later, when it is more widely and reliably available, it may be more useful.
Do not confuse DOM NodeList arrays with Enumerator or Collection objects. The NodeListitem() method is subtly different to the Enumerator.Item() method.
When tested on MSIE 5 for Macintosh, this object exhibited some very odd behavior.
When passed a FormArray, it complained that it was not a Collection object. When passed a Collection object (document.all) it still complained. However, when passed an Array object, it was happy to accept it. It would allow a new Enumerator to be created with no argument being passed to its constructor function.
When examined, its constructor reported that it was a reference to a Date object.
The object may only be usable on MSIE on the Windows platform until a later version of MSIE supports a corrected implementation.
The naming convention for methods and properties of this object are capitalized in a very untypical way and you need to be aware of this in case you have trouble getting your enumerator to work properly.
// Instantiate a file system object myFileSystem = new ActiveXObject("Scripting.FileSystemObject"); // Create an enumerator myEnum = new Enumerator(myFileSystem.Drives); // Traverse the Drives collection via the enumerator for(; !myEnum.atEnd(); myEnum.moveNext()) { processDrive(myEnum.item()); } // A function to do something with each disk drive function processDrive(aDrive) { ... }
See also: | Collection object, Files object, NodeList object |
Property | JavaScript | JScript | N | IE | Opera | NES | ECMA | DOM | CSS | HTML | Notes |
---|---|---|---|---|---|---|---|---|---|---|---|
constructor | ![]() | 3.0 ![]() | ![]() | 4.0 ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
Method | JavaScript | JScript | N | IE | Opera | NES | ECMA | DOM | CSS | HTML | Notes |
---|---|---|---|---|---|---|---|---|---|---|---|
atEnd() | ![]() | 3.0 ![]() | ![]() | 4.0 ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | - |
item() | ![]() | 3.0 ![]() | ![]() | 4.0 ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | - |
moveFirst() | ![]() | 3.0 ![]() | ![]() | 4.0 ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | - |
moveNext() | ![]() | 3.0 ![]() | ![]() | 4.0 ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | - |
Prev | Home | Next |
Enumeration constant | Up | Enumerator() |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |