다음을 통해 공유


Enumerator 개체

컬렉션에 있는 항목을 열거할 수 있도록 합니다.

varName = new Enumerator([collection])

인수

  • varName
    필수적 요소로서, 열거자를 할당할 변수 이름입니다.

  • collection
    선택적 요소로서, 배열 또는 컬렉션과 같이 IEnumerable 인터페이스를 구현하는 개체입니다.

설명

JScript에서는 모든 컬렉션을 자동으로 열거할 수 있습니다. 따라서 Enumerator 개체를 사용하여 컬렉션 멤버에 액세스할 필요 없이 for...in 문을 사용하여 모든 멤버에 직접 액세스할 수 있습니다. Enumerator 개체를 사용하면 이전 버전과 호환됩니다.

컬렉션은 멤버에 직접 액세스할 수 없다는 점에서 배열과 다릅니다. 배열에서처럼 인덱스를 사용하는 대신 현재 항목 포인터를 컬렉션의 첫 번째 요소나 다음 요소로만 옮길 수 있습니다.

컬렉션의 모든 멤버에 액세스하는 방법을 제공하는 Enumerator 개체는 VBScirpt의 For...Each 문과 유사하게 작동합니다.

IEnumerable를 구현하는 클래스를 정의하여 JScript에서 컬렉션을 만들 수 있습니다. Visual Basic 등의 다른 언어를 사용하거나 ActiveXObject 개체를 사용하여 컬렉션을 만들 수도 있습니다.

예제 1

다음 코드에서는 Enumerator 개체를 사용하여 사용 가능한 드라이브가 있는 경우 드라이브 문자와 이름을 인쇄합니다.

// Declare variables.
var n, x;
var fso : ActiveXObject = new ActiveXObject("Scripting.FileSystemObject");
// Create Enumerator on Drives.
var e : Enumerator = new Enumerator(fso.Drives);
for (;!e.atEnd();e.moveNext()) {      // Loop over the drives collection.
   x = e.item();
   if (x.DriveType == 3)              // See if network drive.
      n = x.ShareName;                // Get share name
   else if (x.IsReady)                // See if drive is ready.
      n = x.VolumeName;               // Get volume name.
   else
      n = "[Drive not ready]";
   print(x.DriveLetter + " - " + n);
}

시스템에 따라 다음과 같이 출력될 수 있습니다.

A - [Drive not ready]
C - DRV1
D - BACKUP
E - [Drive not ready]

예제 2

예제 1의 코드는 Enumerator 개체를 사용하지 않고 작동할 수 있도록 다시 작성할 수 있습니다. 다음과 같이 작성하면 열거형 멤버에 직접 액세스할 수 있습니다.

// Declare variables.
var n, x;
var fso : ActiveXObject = new ActiveXObject("Scripting.FileSystemObject");
// The following three lines are not needed.
//    var e : Enumerator = new Enumerator(fso.Drives);
//    for (;!e.atEnd();e.moveNext()) {
//       x = e.item();
// Access the members of the enumeration directly.
for (x in fso.Drives) {               // Loop over the drives collection.
   if (x.DriveType == 3)              // See if network drive.
      n = x.ShareName;                // Get share name
   else if (x.IsReady)                // See if drive is ready.
      n = x.VolumeName;               // Get volume name.
   else
      n = "[Drive not ready]";
   print(x.DriveLetter + " - " + n);
}

속성

Enumerator 개체에는 속성이 없습니다.

메서드

Enumerator 개체 메서드

요구 사항

버전 3

참고 항목

참조

new 연산자

for...in 문