这段代码会产生一个错误:
function *giveNumbers() {
[1, 2, 3].forEach(function(item) {
yield item;
})
}
这可能是因为yield语句在一个不是生成器的函数中。有没有更优雅的方法来解决这个问题?我是指除了以下方法:
function *giveNumbers() {
let list = [1, 2, 3];
for (let i = 0; i < list.length; i++) {
yield list[i];
}
}
forEach
应该在 ES6 中被删除。 - BergiforEach
在ES6中肯定会被淘汰”。 - PalisandforEach
应该在 ES6 中被淘汰 - Bergi