Javascript Iterator Create Array Iterator
class ArrayIterator {//from ww w .j av a 2s. c o m constructor(array) { this.array = array.map(item => item).sort(); this.index = 0; } next() { let result = { value: undefined, done: true }; if (this.index < this.array.length) { result.value = this.array[this.index]; result.done = false; this.index++; } return result; } } class TaskList { constructor() { this.tasks = []; } addTasks(...tasks) { this.tasks = this.tasks.concat(tasks); } [Symbol.iterator]() { return new ArrayIterator(this.tasks); } } let taskList = new TaskList(); taskList.addTasks('Learn JS', 'Learn ES6', 'Buy products'); for (let task of taskList) { console.log(task); }