Skip to content

Instantly share code, notes, and snippets.

@taiju
Created January 11, 2011 05:21
Show Gist options
  • Save taiju/774054 to your computer and use it in GitHub Desktop.
Save taiju/774054 to your computer and use it in GitHub Desktop.
クロージャ版iterator
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