Skip to content

Commit 7e9bef2

Browse files
committed
Itrator Pattern
1 parent d508bd6 commit 7e9bef2

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Example/Iterator-Pattern.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1-
// 迭代器模式(Iterator Pattern)
1+
// 迭代器模式(Iterator Pattern)
2+
3+
let each = (arr, callBack) => {
4+
for (let i = 0; i < arr.length; i++) {
5+
if (callBack && callBack(i, arr[i]) === false) {
6+
break;
7+
}
8+
}
9+
}
10+
11+
let arr = [1, 2, 3, 4]
12+
each(arr, (index, value) => {
13+
if (value > 2) return false
14+
console.log(index, value)
15+
})

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@
113113

114114
> [迭代器模式(Iterator Pattern)](/Example/Iterator-Pattern.js)
115115
116+
定义:用于顺序访问集合对象的元素,不需要知道集合对象的底层表示。
117+
目的:提供一种方法顺序访问一个聚合对象中各个元素, 而又无须暴露该对象的内部表示。
118+
场景:$.each() for..of。
119+
116120
> [命令模式(Command Pattern)](/Example/Command-Pattern.js)
117121
118122
> [职责链模式(Chain of Responsibility Pattern)](/Example/Chain-Of-Responsibility-Pattern.js)

0 commit comments

Comments
 (0)