当时是懵逼的,后面上网查也查不到。
只能认真看ES6文档。
直接上代码吧。
class RangeIterator {
constructor(year) {
this.count = 0
this.year = year
this.fullDays = year % 4 === 0 ? 366 : 365
this.firstDay = new Date(`${year}-01-01 00:00:00`)
}
[Symbol.iterator]() {
return this
}
next() {
if (this.count <= this.fullDays) {
let now = new Date(this.firstDay)
now.setDate(this.count)
this.count ++
return {
value: `${now.getMonth() + 1}.${now.getDate()}`,
done: false
}
}
return {
value: undefined,
done: true
}
}
}
function range (year) {
return new RangeIterator(year)
}
for (let iter of range(2020)) {
console.log(iter)
}