zweizhao.github.io

项目,文章,随笔,博客,Markdown,个人网站,人生感悟。

View on GitHub

迭代器实例代码

function *gFunc() {
    console.log(0)
	yield 1
    console.log(2)
	yield 3
    console.log(4)
	yield 5
    console.log(6)
}

let gg = gFunc()
gg.next()
// 0
// {value: 1, done: false}
gg.next()
// 2
// {value: 3, done: false}
gg.next()
// 4
// {value: 5, done: false}
gg.next()
// 6
// {value: undefined, done: true}
gg.next()
// {value: undefined, done: true}