JavaScript 中的 “function*”是什么?
function* 声明用于定义生成器函数。它返回一个生成器对象。生成器函数允许在函数退出并在稍后恢复时执行代码。因此,生成器可用于管理代码中的流控制。
语法
以下是语法
function *myFunction() {}
// or
function* myFunction() {}
// or
function*myFunction() {}我们来看看如何使用生成器函数
示例
<html>
<body>
<script>
function* display() {
var num = 1;
while (num < 5)
yield num++;
}
var myGenerator = display();
document.write(myGenerator.next().value);
document.write("<br>"+myGenerator.next().value);
document.write("<br>"+myGenerator.next().value);
document.write("<br>"+myGenerator.next().value);
document.write("<br>"+myGenerator.next().value);
</script>
</body>
</html>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP