I understand most of the following code, but I'm completely stuck on [...numbers[Symbol.iterator].
var numbers = {
*[Symbol.iterator]({
start = 0,
stop = 100,
step = 1
} = {}) {
for(let x = start; x <= stop; x+=step) {
yield x;
}
}
};
// should print 0.. 100 in steps of 1
for(let num of numbers) {
console.log(num);
}
// should print 6..30 in steps of 4
console.log(`My lucky numbers are: ${
[...numbers[Symbol.iterator]({
start: 6,
stop: 30,
step: 4
})]}`
);
In my mind:
If it was just [...numbers] it would spread the numbers 0 - 100 because the object has an iterator function/generator.
If it was just numbers[Symbol.iterator]() it would look up and return the generator.
But I've no clue with [...numbers[Symbol.iterator](...), which looks like a strange combination of the two.
I'm struggling to get results when googling this syntax. If anyone could explain it like I'm 5 that would be much appreciated!