Generator
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2016ë 9ì.
Generator ê°ì²´ë generator function ì¼ë¡ë¶í° ë°íëë©°, ë°ë³µ ê°ë¥í íë¡í ì½ê³¼ ë°ë³µì íë¡í ì½ì 모ë ì¤ìí©ëë¤.
Constructor
ì´ ê°ì²´ë ë°ë¡ ì¸ì¤í´ì¤íí ë ì ììµëë¤. ëì ì ëë ì´í° í¨ì를 íµí´ Generator ì¸ì¤í´ì¤ë¥¼ ë°íí ì ììµëë¤.
function* generator() {
yield 1;
yield 2;
yield 3;
}
const gen = generator(); // "Generator { }"
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
console.log(gen.next().value); // 3
ì¸ì¤í´ì¤ ë©ìë
Generator.prototype.next()-
yieldííìì íµí´ yieldë ê°ì ë°íí©ëë¤. Generator.prototype.return()-
주ì´ì§ ê°ì ë°ííê³ ì ëë ì´í°ë¥¼ ì¢ ë£í©ëë¤.
Generator.prototype.throw()-
ì ëë ì´í°ì ì¤ë¥ë¥¼ ë°ììíµëë¤. (í´ë¹ ì ëë ì´í° ë´ìì ì¤ë¥ê° ë°ìí ê²½ì°ê° ìë í ì ëë ì´í°ë ìë£)
ìì
>무í ì ëë ì´í°
ì ëë ì´í° í¨ì를 ì¬ì©íë©´ ê°ì íìí ëê¹ì§ ê³ì°ëì§ ììµëë¤. ë°ë¼ì ì ëë ì´í°ë ì ì¬ì ì¼ë¡ 무íí ë°ì´í° 구조를 ì ìí ì ììµëë¤.
function* infinite() {
let index = 0;
while (true) {
yield index++;
}
}
const generator = infinite(); // "Generator { }"
console.log(generator.next().value); // 0
console.log(generator.next().value); // 1
console.log(generator.next().value); // 2
// ...
ëª ì¸ì
| Specification |
|---|
| ECMAScript® 2027 Language Specification > # sec-generator-objects > |