forked from zoltantothcom/Design-Patterns-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbehavioral_iterator.js
More file actions
42 lines (36 loc) · 923 Bytes
/
behavioral_iterator.js
File metadata and controls
42 lines (36 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const ITERATOR = {
id: 'iterator',
name: 'Iterator',
type: 'behavioral',
hint: 'Sequentially access the elements of a collection',
definition: `Provide a way to access the elements of an aggregate object sequentially
without exposing its underlying representation.`,
when: "you want to access object's content without knowing how it is internally represented",
codeES5: `function Pattern(el) {
this.index = 0;
this.elements = el;
}
Pattern.prototype = {
next: function() {
return this.elements[this.index++];
},
hasNext: function() {
return this.index < this.elements.length;
}
};
module.exports = Pattern;`,
codeES6: `class Pattern {
constructor(el) {
this.index = 0;
this.elements = el;
}
next() {
return this.elements[this.index++];
}
hasNext() {
return this.index < this.elements.length;
}
}
export default Pattern;`
};
export default ITERATOR;