We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
生成器对象是由一个generator function返回的,并且它符合可迭代协议和迭代器协议
generator function
语法:
function* gen() { yield 1; yield 2; yield 3; } let g = gen();
iterator
yield
next()
value
done
function* Van(arr = []) { for(const v of arr) { yield v } } const van = Van([3, 5, 8, 10]) console.log(van.next().value) // 3 console.log(van.next().value) // 5 console.log(van.next().value) // 8 console.log(van.next().value) // 10
function* Van() { yield 2; } const van = new Van() // Uncaught TypeError: Van is not a constructor
用于向生成器抛出一个错误
function* gen() { while(true) { try { yield 42; } catch(e) { console.log("Error caught!"); } } } var g = gen(); g.next(); // { value: 42, done: false } g.throw(new Error("Something went wrong")); // "Error caught!"
The text was updated successfully, but these errors were encountered:
No branches or pull requests
语法:
iterator
)对象yield
的位置为止,yield
后紧跟迭代器需返回的值next()
方法返回一个对象,这个对象包含两个属性:value
和done
,value
属性表示本次yield
表达式的返回值,done
属性为布尔类型,表示生成器后续是否还有yield
语句,即生成器函数是否已经执行完毕并返回next()
方法时,如果传入了参数,那么这个参数会传给上一条执行的yield
语句左边的变量,假如上一条执行的语句没有左边变量,则传参无效生成器可以传入参数
生成器函数不能当作构造函数使用
generator.throw
The text was updated successfully, but these errors were encountered: