public
Created

  • Download Gist
step-through
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
var arr = [1, "2", null, 4, undefined, 6, 7, 8];
 
var j = {
step: function (arr) {
var current = -1;
var len = arr.length;
var err = new Error("End of list");
return {
current: function () {
if (current < 0 || current >= len) {
throw err;
} else {
return arr[current];
}
},
next: function () {
current += 1;
return this.current();
},
previous: function () {
current -= 1;
return this.current();
}
}
}
};
 
var iter = j.step(arr);
console.log(iter.next());
console.log(iter.next());
console.log(iter.next());
console.log(iter.next());
console.log(iter.next());
console.log(iter.next());
console.log(iter.next());
console.log(iter.next());
console.log(iter.next());

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.