当前位置:网站首页>(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"?

这个mySQL安装的时候怎么搞去了?

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

真正的测试 =“半个产品+半个开发”?

由ASP.NET Core根据路径下载文件异常引发的探究

每日刷题记录 (七)

(JS)数组去除重复

With this tool, automatic identification and verification code is no longer a problem

深入浅出总结Flink运行时架构

Fully understand the volatile keyword
随机推荐
全面理解Synchronized
Print 9*9 multiplication formula table (C language)
[FreeRTOS] 08 mutex semaphores and priority inversion
Alibaba cloud server is installed and configured with redis. Remote access is unavailable
Given the values of two integer variables, the contents of the two values are exchanged (C language)
Print leap years between 1000 and 2000 (C language)
30岁,女,普通软件测试媛,对职业的迷茫和焦虑
Report card of regional industrial Internet market, the second place of Baidu intelligent yunkaiwu
基于STM32+RFID设计的宿舍检修管理系统
Real test = "half product + Half development"?
Memory allocation - static storage stack heap and static variable
Does anyone encounter this problem when flinkcdc synchronizes MySQL?
Software test model (V model and W model)
Several methods of enterprise competition analysis: SWOT, Porter five forces, pest "suggestions collection"
flink sql cdc 并行度 会不会影响顺序呀,是不是数据同步的话一般只能设置1。
“AI x 科学计算”进行时,华为昇思 MindSpore 赛题火热开启,等你来!
IO流总结
Getting started with the lvgl Library - Animation
最后的 48 小时!云 XR 专题赛邀你一起绽放精彩,我们赛场见!
VI exit exit VIM applicable novice