Skip to content

Instantly share code, notes, and snippets.

@ducin
Created August 6, 2013 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ducin/6163340 to your computer and use it in GitHub Desktop.
Save ducin/6163340 to your computer and use it in GitHub Desktop.
stateful functions - generating unique id for objects and iterating collections
(function () {
function iterator(collection) {
var index = 0;
var length = collection.length;
function next() {
var item = collection[index++];
return item;
}
function hasNext() {
return index < length;
}
return {
next: next,
hasNext: hasNext
};
}
if (typeof Tools == "object") {
Tools.iterator = iterator;
}
}());
var Tools = {};
(function () {
var id = 0;
function uid(object) {
if (typeof object.__uid != "number") {
object.__uid = id++;
}
return object.__uid;
}
if (typeof Tools == "object") {
Tools.uid = uid;
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment