이 부분이 논란이 있다.
Symbol.iterator
라는 키로 함수가 구현되어 있어야 한다. 이런 것들을 이터러블 하다고 한다.대에충 만든 커스텀 이터러블
class CustomIterable implements Iterable<string> {
name: string = 'max';
index: number = 0;
[Symbol.iterator]() {
const iterator: Iterator<string> = {
next: () => ({
value: this.name[this.index++],
done: !this.name[this.index - 1],
}),
};
return iterator;
}
}
const iter = new CustomIterable();
for (const str of iter) {
console.log('hello', str);
}
커스텀 이터러블을 for of
에서 쓰려면 컴파일 설정이 필요하다.
"downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */