forked from robdodson/JavaScript-Design-Patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
35 lines (31 loc) · 807 Bytes
/
main.js
File metadata and controls
35 lines (31 loc) · 807 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
// Examples inspired by Stoyan Stefanov and his
// amazing book JavaScript Patterns
// http://www.amazon.com/JavaScript-Patterns-Stoyan-Stefanov/dp/0596806752
"use strict";
var iterator = (function() {
var data = { foo: 'foo', bar: 'bar', baz: 'baz' },
keys = Object.keys(data),
index = 0,
length = keys.length;
return {
next: function() {
var element;
if (!this.hasNext()) {
return null;
}
element = data[keys[index]];
index++;
return element;
},
hasNext: function() {
return index < length;
},
rewind: function() {
index = 0;
return data[keys[index]];
},
current: function() {
return data[keys[index]];
}
};
}());