设计模式:迭代器模式
实现迭代器的目的是干什么呢?
// 迭代器设计模式
// 场景:你和我协同开发一个任务清单应用
// 我的代码 =========
const todoList = {
life:['吃饭','睡觉','撸猫'],
study:['语文','数学','英语']
}
// 你的代码 =========
for (const item of todoList.life) {
console.log(item);
}
for (const item of todoList.study) {
console.log(item);
}
如果我需要在我的todoList 数据列表里添加数据时,你的代码就需要作出相应的更改,对于调用来说,代码耦合度太高
如下:
// 迭代器设计模式