Created
January 11, 2011 05:21
-
-
Save taiju/774054 to your computer and use it in GitHub Desktop.
クロージャ版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 iterator = function(arr) { | |
if (!arr.length) throw new TypeError(typeof arr + ' is not permitted.'); | |
var copy = (arr instanceof Array) ? arr.slice() : Array.prototype.slice.call(arr); | |
var len = copy.length; | |
var _move = function(method_name) { | |
var current = copy[method_name](); | |
len--; | |
if (len === -1) throw new Error('Stop Iteration'); | |
return current; | |
}; | |
return { | |
next: function() { | |
return _move('shift'); | |
}, | |
prev: function() { | |
return _move('pop'); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment