当前位置:网站首页>(JS)迭代器模式
(JS)迭代器模式
2022-06-29 09:57:00 【不愿透露姓名的余菜鸟】
(JS)迭代器模式
顺序访问一个集合
使用者无需知道集合内部的结构
class Iterator {
constructor(container) {
this.list = container.list;
this.index = 0;
}
next() {
if(this.hasNext()) {
return this.list[this.index++];
}
return null;
}
hasNext() {
if(this.index>=this.list.length) {
return false;
}
return true;
}
}
class Container {
constructor(list) {
this.list = list;
}
getIterator() {
return new Iterator(this)
}
}
var arr = [1,2,3,4,5,6,7];
var container = new Container(arr);
var iterator = container.getIterator();
while (iterator.hasNext()) {
console.log(iterator.next())
}
边栏推荐
- Real test = "half product + Half development"?
- What happened during the MySQL installation?
- Redis installation and cluster setup under Linux
- Xiaomi mobile phone - Unlock BL + open root permission
- Hystrix fuse: Service fusing and service degradation
- Please tell me about the Flink SQL batch task, two or more tables join (inner join or outer join
- The difference between & & and &
- 基于支持向量机的手写数字识别详解(MATLAB GUI代码,提供手写板)
- 在Clion中使用EasyX配置
- Does anyone encounter this problem when flinkcdc synchronizes MySQL?
猜你喜欢

STM32F1與STM32CubeIDE編程實例-超聲波測距傳感器驅動

【C语言进阶】自定义类型

AQS之Atomic详解

全面理解Volatile关键字

《MongoDB入门教程》第02篇 MongoDB安装

UserWarning: Usage of dash-separated ‘script-dir‘ will not be supported in future versions. note

罗清启:高端家电已成红海?卡萨帝率先破局

美国EB-5移民再现利好,区域中心再授权政策被叫停

共2600页!又一份神级的面试手册面世~

FreeRTOS porting of official website based on keil5 auto configuring STM32F103 standard library
随机推荐
Common motor classification and driving principle animation [easy to understand]
np.astype()
Fully understand the MESI cache consistency protocol
世界上第一个“半机械人”去世,改造自己只为“逆天改命”
CS231n-2022 Module1: 神经网络要点概述(2)
罗清启:高端家电已成红海?卡萨帝率先破局
【C语言进阶】文件操作(一)
全面理解MESI缓存一致性协议
ZABBIX monitors various MySQL indicators
Excel日期及数字格式处理
flink sql cdc 并行度 会不会影响顺序呀,是不是数据同步的话一般只能设置1。
Print 9*9 multiplication formula table (C language)
【C语言进阶】文件操作(二)
历史上的今天:马斯克出生;微软推出 Office 365;蔡氏电路的发明者出生
加密市场接连爆雷,Celsius能避免破产吗?
arcgis创建postgre企业级数据库
# 【OpenCV 例程200篇】214. 绘制椭圆的参数详解
他98年的,我玩不过他...
STM32F1與STM32CubeIDE編程實例-超聲波測距傳感器驅動
30岁,女,普通软件测试媛,对职业的迷茫和焦虑