Baseline Widely available
function*
ì ì¸ (ëì ë³íê° ìë function
keyword) ì generator function ì ì ìíëë°, ì´ í¨ìë Generator
ê°ì²´ë¥¼ ë°íí©ëë¤.
function* generator(i) {
yield i;
yield i + 10;
}
const gen = generator(10);
console.log(gen.next().value);
// Expected output: 10
console.log(gen.next().value);
// Expected output: 20
generator function ì GeneratorFunction
ìì±ìì function* expression
ì ì¬ì©í´ì ì ìí ì ììµëë¤.
function* name([param[, param[, ... param]]]) {
statements
}
name
í¨ìëª .
param
í¨ìì ì ë¬ëë ì¸ìì ì´ë¦. í¨ìë ì¸ì를 255ê°ê¹ì§ ê°ì§ ì ìë¤.
statements
í¨ìì 본체를 구ì±íë 구문ë¤.
Generatorë ë¹ ì ¸ëê°ë¤ê° ëì¤ì ë¤ì ëìì¬ ì ìë í¨ìì ëë¤. ì´ë 컨í ì¤í¸(ë³ì ê°)ë ì¶ì ê³¼ì ìì ì ì¥ë ìíë¡ ë¨ì ììµëë¤.
Generator í¨ìë í¸ì¶ëì´ë ì¦ì ì¤íëì§ ìê³ , ëì í¨ì를 ìí Iterator ê°ì²´ê° ë°íë©ëë¤. Iteratorì next()
ë©ìë를 í¸ì¶íë©´ Generator í¨ìê° ì¤íëì´ yield
문ì ë§ë ëê¹ì§ ì§ííê³ , í´ë¹ ííìì´ ëª
ìíë Iteratorë¡ë¶í°ì ë°íê°ì ë°íí©ëë¤. yield*
ííìì ë§ì£¼ì¹ ê²½ì°, ë¤ë¥¸ Generator í¨ìê° ìì(delegate)ëì´ ì§íë©ëë¤.
ì´í next()
ë©ìëê° í¸ì¶ëë©´ ì§íì´ ë©ì·ë ìì¹ììë¶í° ì¬ì¤íí©ëë¤. next()
ê° ë°ííë ê°ì²´ë yield
ë¬¸ì´ ë°íí ê°(yielded value)ì ëíë´ë value
ìì±ê³¼, Generator í¨ì ìì 모ë yield
문ì ì¤í ì¬ë¶ë¥¼ íìíë boolean íì
ì done
ìì±ì ê°ìµëë¤. next()
를 ì¸ìê°ê³¼ í¨ê» í¸ì¶í ê²½ì°, ì§íì ë©ì·ë ìì¹ì yield
문ì next()
ë©ìëìì ë°ì ì¸ìê°ì¼ë¡ ì¹ííê³ ê·¸ ìì¹ìì ë¤ì ì¤ííê² ë©ëë¤.
function* idMaker() {
var index = 0;
while (index < 3) yield index++;
}
var gen = idMaker();
console.log(gen.next().value); // 0
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
console.log(gen.next().value); // undefined
// ...
yield* 를 ì¬ì©í ìì
function* anotherGenerator(i) {
yield i + 1;
yield i + 2;
yield i + 3;
}
function* generator(i) {
yield i;
yield* anotherGenerator(i);
yield i + 10;
}
var gen = generator(10);
console.log(gen.next().value); // 10
console.log(gen.next().value); // 11
console.log(gen.next().value); // 12
console.log(gen.next().value); // 13
console.log(gen.next().value); // 20
Generator ì ì¸ìê°ì ë기ë ìì
function* logGenerator() {
console.log(yield);
console.log(yield);
console.log(yield);
}
var gen = logGenerator();
// the first call of next executes from the start of the function
// until the first yield statement
gen.next();
gen.next("pretzel"); // pretzel
gen.next("california"); // california
gen.next("mayonnaise"); // mayonnaise
Generator ë ìì±ìë¡ì ì¬ì©ë ì ìë¤
function* f() {}
var obj = new f(); // throws "TypeError: f is not a constructor"
ëª
ì¸ì ë¸ë¼ì°ì í¸íì± ê´ë ¨ í목
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4