계속 next로 호출할 수 있으나 끝까지 진행하면 뒤로 돌어가서 다른 데이터를 제공할 수는 없음
const book = [
"Twinkle, twinkle, little star",
"How I wonder what you are",
"Up above the world so high",
"Like a diamond in the sky",
"Twinkle, twinkle little star",
"How I wonder what you are"
];
const it = book.values(); // 이터레이터 생성
console.log(it.next()); // {value: "Twinkle, twinkle, little star", done: false}
console.log(it.next()); // {value: "How I wonder what you are", done: false}
console.log(it.next()); // {value: "Up above the world so high", done: false}
console.log(it.next()); // {value: "Like a diamond in the sky", done: false}
console.log(it.next()); // {value: "Twinkle, twinkle little star", done: false}
console.log(it.next()); // {value: "How I wonder what you are", done: false}
console.log(it.next()); // {value: undefined, done: true}
console.log(it.next()); // {value: undefined, done: true}
console.log(it.next()); // {value: undefined, done: true}
console.log(it.next()); // {value: undefined, done: true}
2) 독립적
새 이터레이터를 만들 때마다 처음에서 시작
각각 다른 쇼로르 가리키는 이터레이터 여러 개를 동시에 사용할 수 있음
const book = [
"Twinkle, twinkle, little star",
"How I wonder what you are",
"Up above the world so high",
"Like a diamond in the sky",
"Twinkle, twinkle little star",
"How I wonder what you are"
];
// 이터레이터 생성
const it1 = book.values();
const it2 = book.values();
// it1 으로 두 페이지 읽음
console.log(it1.next()); // {value: "Twinkle, twinkle, little star", done: false}
console.log(it1.next()); // {value: "How I wonder what you are", done: false}
// it2 로 한 페이지 읽음
console.log(it2.next()); // {value: "Twinkle, twinkle, little star", done: false}
// it1 으로 한 페이지 더 읽음
console.log(it1.next()); // {value: "Up above the world so high", done: false}