Created
September 10, 2010 21:43
-
-
Save mseeley/574418 to your computer and use it in GitHub Desktop.
Encapsulated array walking
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 Set (/* varargs */) { | |
var i = 0, | |
items = [].slice.call(arguments, 0); | |
this.item = function (idx) { | |
var item = items[i]; | |
if (idx != null) { | |
if (idx in items) { | |
i = idx; | |
} | |
item = items[idx]; | |
} | |
return item; | |
}; | |
this.next = function (wrap) { | |
var len = items.length; | |
i = i >= len && wrap ? 0 : i + 1; | |
return i < len; | |
}; | |
this.prev = function (wrap) { | |
i = (wrap && i <= 0 ? items.length : i) - 1; | |
return i >= 0; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment