JavaScript中的yield关键字是什么? [英] What's the yield keyword in JavaScript?

查看:296
本文介绍了JavaScript中的yield关键字是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaScript中听说过yield关键字,但我发现它的文档很差。有人可以解释我(或推荐一个解释它的用途的网站)以及它的用途吗?

I heard about a "yield" keyword in JavaScript, but I found very poor documentation about it. Can someone explain me (or recommend a site that explains) its usage and what it is used for?

推荐答案

MDN文档非常好,IMO。


包含yield关键字的函数是一个生成器。当你调用它时,它的形式参数绑定到实际的参数,但它的实体并没有被实际评估。而是返回一个generator-iterator。每次调用generator-iterator的next()方法都会执行迭代算法的另一次传递。每个步骤的值是yield关键字指定的值。将yield视为返回的generator-iterator版本,指示算法的每次迭代之间的边界。每次调用next()时,生成器代码将从yield之后的语句中恢复。

The function containing the yield keyword is a generator. When you call it, its formal parameters are bound to actual arguments, but its body isn't actually evaluated. Instead, a generator-iterator is returned. Each call to the generator-iterator's next() method performs another pass through the iterative algorithm. Each step's value is the value specified by the yield keyword. Think of yield as the generator-iterator version of return, indicating the boundary between each iteration of the algorithm. Each time you call next(), the generator code resumes from the statement following the yield.

这篇关于JavaScript中的yield关键字是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆