当前位置:网站首页>(JS) iterator mode
(JS) iterator mode
2022-06-29 11:11:00 【Yu Cainiao, who asked not to be named】
(JS) Iterator pattern
Sequential access to a collection
The consumer does not need to know the internal structure of the collection
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())
}
边栏推荐
- 【NLP】文本生成专题1:基础知识
- PyTorch学习笔记(6)——DataLoader源代码剖析
- nuc980 已成功启动
- Shell 引号和转义从来很少被人注意,但平时写脚本又经常用
- (JS)职责链模式
- math_ Mathematical expression & deformation of equation equation & accumulation of combined operation skills / means
- 最后的 48 小时!云 XR 专题赛邀你一起绽放精彩,我们赛场见!
- Stm32f1 and stm32subeide programming example - ultrasonic distance sensor drive
- 在编写shell脚本时如何正确姿势地管理临时文件
- Online sql to htmltable tool
猜你喜欢

在Clion中使用EasyX配置

BS-GX-018 基于SSM实现在校学生考试系统

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

Limit introduction summary

【各种**问题系列】OLTP和OLAP是啥?

The encryption market has exploded one after another. Can Celsius avoid bankruptcy?

世界上第一个“半机械人”去世,改造自己只为“逆天改命”

如何通过WinDbg获取方法参数值

Multi thread communication between client and server (primary version)

What happened during the MySQL installation?
随机推荐
(JS)数组去除重复
Highly paid programmers & interview questions: how to ensure the data consistency between redis cache and database in series 117?
Using EasyX configuration in clion
Mastering the clever use of some shell wildcards will make us write with half the effort
(JS)手写深比较
容器平台性能如何测试,稳定性、扩展效率、组件性能
(JS)模仿一个instanceof方法
Spark - one to one correspondence between task and partition and detailed explanation of parameters
常见电机分类和驱动原理动画[通俗易懂]
Multi thread communication between client and server (primary version)
BS-GX-017基于SSM实现的在线考试管理系统
[digital signal modulation] realize signal modulation and demodulation based on am+fm+dsb+ssb, including Matlab source code
(JS)数组中纯函数
由ASP.NET Core根据路径下载文件异常引发的探究
(JS) observer mode
掌握一些shell 通配符巧妙的运用,会让我们写脚本事半功倍
Ningde era Kirin battery has greater ambition
Common motor classification and driving principle animation [easy to understand]
What happened during the MySQL installation?
Spark - Task 与 Partition 一一对应与参数详解