Skip to content

Instantly share code, notes, and snippets.

@Spencer-Easton
Created December 16, 2016 21:28
Show Gist options
  • Save Spencer-Easton/e9a24452876b6e5d4abe7560a8bf0820 to your computer and use it in GitHub Desktop.
Save Spencer-Easton/e9a24452876b6e5d4abe7560a8bf0820 to your computer and use it in GitHub Desktop.
A stupid simple Iterator Service
var IteratorService_ = function(array){
var newArray = array.slice(0);
Object.defineProperty(newArray, 'nextIndex', { value: 0, enumerable: false, writable:true });
Object.defineProperty(newArray, 'next', { value: function(){if(this.nextIndex >= this.length){throw new Error("NoSuchElementException")}else{return this[this.nextIndex++]}}, enumerable: false });
Object.defineProperty(newArray, 'hasNext', { value: function(){return (this.nextIndex < this.length)}, enumerable: false });
return newArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment