class iteratorWithoutG {
[Symbol.iterator]() {
let val = 0;
let threshold = 10;
return {
next() {
if(val >= threshold) {
console.log('iteratorWithoutG is finished');
return {
done: true,
value: undefined
}
}
return {
done: false,
value: val++
}
},
return() {
return {
done: true,
value: undefined
}
}
}
}
}
const iWithoutG = new iteratorWithoutG(10);
console.log([...iWithoutG]);
I was learning things about the JS iterator, and I got this:
iteratorWithoutG is finished
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
I wondered why the String was displayed before the Array, so I added some logs:
class iteratorWithoutG {
[Symbol.iterator]() {
let val = 0;
let threshold = 10;
return {
next() {
if(val >= threshold) {
console.log('iteratorWithoutG is finished');
console.log(Date.now())
return {
done: true,
value: undefined
}
}
return {
done: false,
value: (val++, Date.now())
}
},
return() {
return {
done: true,
value: undefined
}
}
}
}
}
This time I get this message:
iteratorWithoutG is finished
1610466807875
[ 1610466807872,
1610466807872,
1610466807872,
1610466807872,
1610466807872,
1610466807872,
1610466807872,
1610466807872,
1610466807872,
1610466807872 ]
So the string was in fact generated after the Array, but how can it be displayed before the Array?? Was the Array stored somewhere?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…