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