class Range {
constructor(from, to) {
this.from = from;
this.to = to;
}
has(x) {
return typeof x === 'number' && this.from <= x && x <= this.to;
}
toString() {
return `{ x | ${this.from} <= x <= ${this.to}}`;
}
[Symbol.iterator]() {
let next = Math.ceil(this.from);
let last = this.to;
return {
next() {
return (next <= last) ? { value: next++ } : { done: true }
},
[Symbol.iterator]() {
return this;
}
}
}
}
const obj = new Range(1, 10);
for (let x of obj) {
console.log(x);
}
console.log(obj.has(5));
console.log(obj.toString());