首先最简单的不考虑边界也不太注重性能的做法,基本可以满足绝大多数要求,包括step为负值:
function* range(start, end, step = 1) {
if (end === undefined) {
end = start
start = 0
}
while (start * step < end * step) {
yield start
start += step
}
}
如果是公用函数,需要给出报错信息,且比较注重效率的情况下,可以写成这样:
function* range(start, end, step = 1) {
if (step === 0) {
throw new RangeError('The step of the range cannot be 0')
}
if (end === undefined) {
end = start
start = 0
}
if (typeof start !== 'number' || typeof end !== 'number' || typeof step !== 'number') {
throw new RangeError('Only numbers are accepted as arguments')
}
if (step > 0) {
while (start < end) {
yield start
start += step
}
} else {
while (start > end) {
yield start
start += step
}
}
}
测试结果(数组里面三个点是解构赋值,具体查看MDN解构赋值):
range(5) // [Object]
[...range(5)] // [0,1,2,3,4]
[...range(2, 5)] // [2,3,4]
[...range(0, 5, 2)] // [0,2,4]
[...range(5, 0, -2)] // [5,3,1]
[...range(5, 0, 2)] // []
for(let i of range(5)) { console.log(i) } // 0 1 2 3 4
[...range(0, 5, 0)] // "RangeError: The step of the range cannot be 0"
[...range('5')] // "RangeError: Only numbers are accepted as arguments"
JavaScript没有Python那样丰富的魔法函数,就算用class来定义,也无法做到像Python那样丝滑的直接使用(每次用都要new一下),唯一的好处可能就是方便比较两个不同的range迭代器的状态(因为不能运算符重载,也只能是转成字符串比较),但讲实话这个功能在实际代码中应用确实很少。