/** * 限制长度的数据,可以理解为限制了最大长度的队列 */ export class LimitedArray { readonly limit: number = 5; public items: any[] = []; constructor(limit: number = 5, initial: any[] = []) { this.limit = limit; initial.forEach((item) => { this.push(item); }); } push(...items: any[]) { items.forEach((item) => { this.items.push(item); if (this.items.length > this.limit) { this.items.shift(); } }); return this.items.length; } }