function next(items) { var max = items.length - 1, i = -1; return function () { i = i < max ? i + 1 : 0; return items[i]; }; } var color = next(['red', 'green', 'blue']); alert(color()); alert(color()); alert(color()); alert(color());