xxxxxxxxxx
The iterator is an object that returns values via its method next()
xxxxxxxxxx
const obj = {
start: 0,
end: 100,
step: 5,
};
obj[Symbol.iterator] = function () {
let start = this.start;
const end = this.end;
const step = this.step;
return {
next() {
const o = {
value: start,
done: start > end,
};
start += step;
return o;
},
};
};
for (let v of obj) {
console.log(v);
}