var myCounter = {
max: 0,
position: 0,
get cur() {
if (this.position == this.max) this.position = 0;
this.position += 1;
return this.position;
},
set cur(arg) {
this.max = arg;
}
};
myCounter.cur = 3;
console.log(myCounter.cur);
console.log(myCounter.cur);
console.log(myCounter.cur);
console.log(myCounter.cur);
function next(max) {
var i = 0;
return function () {
i = i < max ? i + 1 : 1;
return i;
};
}
var current = next(3);
console.log(current());
console.log(current());
console.log(current());
console.log(current());