迭代器模式
class Student {
name: string
grade: number //年级
constructor(name) {
this.name = name
}
}
class School {
students: Array = []
static of(…args: never[]): School {
return new School(…(args as []))
}
constructor() {}
*Symbol.iterator {
let i = 0
while (i < this.students.length) {
yield this.students[i++]
}
}
addStudent(stu: Student) {
this.students.push(stu)
}
}
let sch = School.of()
let stus = Array.from(Array(100).keys()).map((row) => {
return new Student(${row}stu
)
})
// console.log(stus.length)
stus.forEach((s) => {
sch.addStudent(s)
})
let i = 0
for (const s of sch) {
}