Created
August 6, 2013 10:12
-
-
Save ducin/6163340 to your computer and use it in GitHub Desktop.
stateful functions - generating unique id for objects and iterating collections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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; | |
} | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Tools = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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