{"id":304,"date":"2020-05-06T14:45:25","date_gmt":"2020-05-06T06:45:25","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=304"},"modified":"2020-05-06T16:51:58","modified_gmt":"2020-05-06T08:51:58","slug":"es6%e4%b8%ad%e7%9a%84generator","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=304","title":{"rendered":"ES6\u4e2d\u7684Generator"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">\u5148\u770b\u4e00\u4e0b\u4e0b\u9762\u7684\u8fd9\u4e2a\u5199\u6cd5<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">function * helloGenerator(){<br>    console.info(\"hello generator!\");<br>}<br>helloGenerator();<br><\/pre>\n\n\n\n<p class=\"has-text-color has-luminous-vivid-orange-color\">\u5982\u679c\u662f\u666e\u901a\u7684\u51fd\u6570\uff0c\u6267\u884chelloGenerator()\u540e\uff0c\u5e94\u8be5\u4f1a\u6253\u5370\u51fahello generator!\uff0c\u5b9e\u9645\u4e0a\uff0c\u8fd9\u91cc\u5e76\u6ca1\u6709\u76f8\u5173\u7684\u65e5\u5fd7\u8f93\u51fa\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u8ba9\u6211\u4eec\u628a\u4f8b\u5b50\u4fee\u6539\u4e00\u4e0b<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">function * helloGenerator(){<br>    console.info(\"hello generator!\");<br>}<br>var h = helloGenerator();<br>h.next();\/\/\u8f93\u51fa\u65e5\u5fd7<br><\/pre>\n\n\n\n<p>h\u8868\u793ahelloGenerator\u65b9\u6cd5\u7684\u4e00\u4e2a\u5f15\u7528\uff0c\u53ef\u4ee5\u8c03\u7528next()\u65b9\u6cd5\u3002\u4e3a\u4ec0\u4e48\u53ef\u4ee5\u8c03\u7528next(),\u662f\u56e0\u4e3aGenerator\u5b9e\u73b0\u4e86\u8fed\u4ee3\u5668Itearator\u63a5\u53e3\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f8b\u5b50\u5728\u6539\u4e00\u4e0b<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/example 1<br>function * helloGenerator(){<br>    yield \"i\";<br>    yield \"am\";<br>    yield \"a\";<br>    yield \"generator\";<br>    return \"done\";<br>}<br>var h = helloGenerator();<br>console.info(h.next()); \/\/{value: \"i\", done: false}<br>console.info(h.next()); \/\/{value: \"am\", done: false}<br>console.info(h.next()); \/\/{value: \"a\", done: false}<br>console.info(h.next()); \/\/{value: \"generator\", done: false}<br>console.info(h.next()); \/\/{value: \"done\", done: true}<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/example 2<br>function * helloGenerator(){<br>    yield \"i\";<br>    yield \"am\";<br>    yield \"a\";<br>    yield \"generator\";<br>    return \"done\";<br>}<br>var h = helloGenerator();<br>for(var v of h){<br>    console.info(v);<br>}<\/pre>\n\n\n\n<p>\u4f8b\u5b501\u4e2d\uff0c\u6bcf\u6b21\u6267\u884cnext()\u65b9\u6cd5\uff0c\u5230\u8fbeyield\u5173\u952e\u5b57\uff0c\u4f1a\u8fd4\u56deyield\u540e\u7684\u5185\u5bb9\uff0c\u5305\u542b\u8fd4\u56de\u503c\u548cgenerator\u7684done\u72b6\u6001\uff0c\u5e76\u6682\u505c\u6574\u4e2a\u51fd\u6570\u7684\u6267\u884c\u3002\u4f8b\u5b502\u4e2d\u5c55\u793a\u4e86\uff0cgenerator\u7684\u8fed\u4ee3\u6027\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6211\u4eec\u53ef\u4ee5\u628ah.next()\u7684\u503c\u63a5\u6536\u540e\uff0c\u4f20\u9012\u7ed9next()\u51fd\u6570\uff0c\u5982\u4e0b\u9762\u7684\u4f8b\u5b50<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">function * helloGenerator(x, y){<br>    let z = yield x+y;<br>    let result = yield z*x;<br>    return result<br>}<br>var h = helloGenerator(3,5);<br>console.info(h.next());\/\/ {value: 8, done: false}<br>console.info(h.next());\/\/ {value: NaN, done: false}<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">function * helloGenerator(x, y){<br>    let z = yield x+y;<br>    let result = yield z*x;<br>    return result<br>}<br>var h = helloGenerator(3,5);<br>var sum = h.next();<br>console.info(h.next(sum.value));<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">function * helloGenerator(x, y){<br>    let z;<br>    let result;<br>    yield z = x+y;<br>    yield result = z*x;<br>    return result<br>}<br>var h = helloGenerator(3,5);<br>console.info(h.next());<br>console.info(h.next());<\/pre>\n\n\n\n<p>\u8fed\u4ee3\u5668\u9664\u4e86\u53ef\u4ee5\u8c03\u7528next()\u65b9\u6cd5\uff0c\u8fd8\u53ef\u4ee5\u8c03\u7528return\u65b9\u6cd5\uff0creturn\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u5e26\u6709\u8fd4\u56de\u503c\u3002return\u8c03\u7528\u4e4b\u540e\uff0c\u540e\u9762\u7684next\uff0c\u5c06\u8fd4\u56deundefined\u3002<\/p>\n\n\n\n<p>generator\u53ef\u4ee5\u65b9\u4fbf\u7684\u8ba9\u51fa\u6216\u8005\u5f97\u5230\u51fd\u6570\u6267\u884c\u6743\u3002\u8fd0\u7528yield\u5173\u952e\u5b57\u548cgenerator\u51fd\u6570\uff0c\u53ef\u4ee5\u4f18\u5316\u5f02\u6b65\u7f16\u7a0b\u7684\u6d41\u7a0b\u63a7\u5236\uff0c\u51cf\u5c11\u56de\u8c03\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5148\u770b\u4e00\u4e0b\u4e0b\u9762\u7684\u8fd9\u4e2a\u5199\u6cd5 function * helloGenerator(){ console.info(&#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/304"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=304"}],"version-history":[{"count":50,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/304\/revisions"}],"predecessor-version":[{"id":354,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/304\/revisions\/354"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}