function show() { console.log('a') console.log('b') } show() // 普通函数 function *show2() { console.log('1') yield console.log('2') } let genObj = show2() genObj.next() // 1 genObj.next() // 2 genObj.next()
需要调用next()方法启动执行,需要遇到 yield 停
generator函数前面加一个 *
两边可以有空格,或靠近函数或function
如果函数前漏掉 *
- 就是普通函数
- 如果有
yield
会报错,ReferenceError: yield is not defined
- yield 只能在Generator函数内部使用