Predict Generator 输出
预测输出 中等
题目
什么做 这个 code print?
代码
function* counter() {
yield 1;
yield 2;
yield 3;
}
const gen = counter();
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
提示
Generators produce 值 one at 时间通过下一个().
解释
Generator 函数 (declared 使用 函数*) produce 值 lazily 通过 yield. 每个 call 到 gen.下一个() resumes execution 直到下一个 yield and 返回 { 值, 做 }. 首先 下一个() yields 1, 其次 yields 2, 第三 yields 3. 每个 .值 是 printed on 它的 own 行.
相关资源
相关课程
- JavaScript 函数
学习关于 generators.
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。