Skip to content

Instantly share code, notes, and snippets.

@arian
Created October 18, 2010 16:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arian/632528 to your computer and use it in GitHub Desktop.
Save arian/632528 to your computer and use it in GitHub Desktop.
Array.Iterator - Iterate trough your arrays
Array.implement({
i: 0,
current: function(){
return this.valid() ? this[this.i] : null;
},
jump: function(i){
if (i < 0) i = -1;
if (i > this.length - 1) i = this.length;
this.i = i;
return this.current();
},
valid: function(){
return (this.i >= 0 && this.i < this.length);
},
key: function(){
return this.valid() ? this.i : false;
},
slide: function(i){
return this.jump(this.i + i);
},
reset: function(){
return this.jump(0);
},
end: function(){
return this.jump(this.length - 1);
},
next: function(){
return this.slide(1);
},
prev: function(){
return this.slide(-1);
}
});
var a = [1, 2, 3, 4, 5];
while (a.valid()){
console.log(a.current());
a.next();
}
var i = [1, 2, 3, 4];
console.log(i);
console.log('current', i.current());
console.log('next', i.next());
console.log('next', i.next());
console.log('next', i.next());
console.log('next', i.next());
console.log('prev', i.prev());
console.log('end', i.end());
console.log('prev', i.prev());
console.log('prev', i.prev());
console.log('next', i.next());
console.log('current', i.current());
console.log('reset', i.reset());
@tlimpanont
Copy link

what do you think about my class? https://gist.github.com/tlimpanont/6547770

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment