Adding a custom iterator to a javascript class
With ES2015 its simple:
function Range(start, end) {
var ret = {};
ret[Symbol.iterator] = function *() {
while (start < end)
yield start++;
}
return ret;
}
Though you have to use:
for (var x of Range(1, 10))
With ES2015 it can be even more simple
const Range = (start, end) => ({
*[Symbol.iterator]() {
while (start < end)
yield start++;
}
})
for (var x of Range(1, 10)) {
console.log(x)
}