Hint
Any object with [Symbol.iterator]() is iterable. It must return an object with a next() method.
const range = {
from: 1, to: 3,
[Symbol.iterator]() {
let cur = this.from;
const last = this.to;
return {
next() {
return cur <= last
? { value: cur++, done: false }
: { value: undefined, done: true };
}
};
}
};
console.log([...range].join(','));1,2,3
Explanation: [Symbol.iterator]() returns a fresh iterator. Spread calls it and collects until done.
Key Insight: Any object with [Symbol.iterator]() is iterable. It must return an object with a next() method.