function* range(start = 0, end = null, step = 1) { if (end == null) { end = start; start = 0; } for (let i = start; i < end; i += step) { yield i; } }
for (let i ofrange(1, 1000000)) { console.log(i); }
常见问题
Visual Studio Code报错
在Visual Studio Code中使用扩展运算符...处理迭代器时,可能会遇到错误:Type 'IterableIterator' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.。解决办法是使用Array.from()替代扩展运算符: