class Range { constructor(from, to) { // 这些属性不是继承的,是当前对象独有的 this.from = from; this.to = to; } includes(x) { return this.from <= x && x <= this.to; } *[Symbol.iterator]() { for (let x = Math.ceil(this.from); x <= this.to; x++) yield x; } toString() { return `(${this.from}...${this.to})`; } } let r = new Range(1, 3); console.log('print:', r.includes(2)); console.log('print:', r.toString()); console.log('iterator:', [...r])