SOURCE

// 使用构造函数的Range类

// 构造函数,不创建和返回对象,只初始化this
function Range(from, to) {
    // 这些属性不是继承的,是当前对象独有的
    this.from = from;
    this.to = to;
}
// 所有对象都继承这个对象,属性必须是prototype
Range.prototype = {
    includes: function (x) { return this.from <= x && x <= this.to; },
    // 实例可迭代,只适用于数值
    [Symbol.iterator]: function* () {
        for (let x = Math.ceil(this.from); x <= this.to; x++) yield x;
    },
    toString: function () { return "(" + this.from + '...' + this.to + ")"; }
}

// 
let r = new Range(1, 3);
console.log('includes:', r.includes(2));
console.log('toString:', r.toString());
console.log('iterator:', [...r])
console 命令行工具 X clear

                    
>
console