{"id":1528,"date":"2020-10-14T15:57:16","date_gmt":"2020-10-14T07:57:16","guid":{"rendered":"https:\/\/www.yusian.com\/blog\/?p=1528"},"modified":"2020-11-09T10:24:08","modified_gmt":"2020-11-09T02:24:08","slug":"es6%e4%b8%ad%e7%9a%84symbol%e3%80%81iterator%e3%80%81generator%e7%9a%84%e7%ae%80%e5%8d%95%e7%90%86%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/javascript\/2020\/10\/14\/1557161528.html","title":{"rendered":"ES6\u4e2d\u7684Symbol\u3001Iterator\u3001Generator\u7684\u7b80\u5355\u7406\u89e3"},"content":{"rendered":"<p>\u2003\u2003\u672c\u6765\u662f\u60f3\u8bf4\u8bf4\u751f\u6210\u5668Generator\u7684\uff0c\u4f46\u6211\u53d1\u73b0\u548c\u8fed\u4ee3\u6709\u5173\uff0c\u6240\u4ee5\u8981\u5148\u8bb2\u8bb2\u8fed\u4ee3\u5668Iterator\uff0c\u53ef\u8fed\u4ee3\u5668\u53c8\u4f1a\u626f\u5230Symbol\uff0c\u6240\u4ee5\u62c9\u51fa\u6765\u4e00\u8d77\u8bf4\u8bf4\uff1b<\/p>\n<h1>Symbol<\/h1>\n<p>Symbol\u662fES6\u5f15\u8fdb\u7684\u4e00\u79cd\u65b0\u6570\u636e\u7c7b\u578b\uff0c\u5b83\u4ee3\u8868\u4e00\u4e2a\u72ec\u4e00\u65e0\u4e8c\u7684\u503c\uff0c\u81f3\u6b64JavaScript\u4e00\u5171\u67097\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5206\u522b\u662f\uff1a<code>Undefinded<\/code>\u3001<code>String<\/code>\u3001<code>Symbol<\/code>\u3001<code>Object<\/code>\u3001<code>Null<\/code>\u3001<code>Number<\/code>\u3001<code>boolean<\/code>\u3002<\/p>\n<h2>Symbol\u662f\u4ec0\u4e48<\/h2>\n<p>Symbol\u5230\u5e95\u662f\u4ec0\u4e48\uff1f\u5148\u6765\u4e2a\u6817\u5b50\u770b\u770b\u6548\u679c\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">let s1 = Symbol();\nlet s2 = Symbol();\nconsole.log(s1, typeof s1); \/\/ Symbol() \"symbol\"\nconsole.log(s1 === s2); \/\/ false\n\nlet s3 = Symbol('symbol');\nlet s4 = Symbol('symbol');\nconsole.log(s3, s4);    \/\/ Symbol(symbol) Symbol(symbol)\nconsole.log(s3 === s4); \/\/ false\n\nlet s5 = Symbol.for('symbol');\nlet s6 = Symbol.for('symbol');\nconsole.log(s5, s6);    \/\/ Symbol(symbol) Symbol(symbol)\nconsole.log(s5 === s6); \/\/ true\nconsole.log(s4 === s5); \/\/ false\n<\/code><\/pre>\n<ul>\n<li>Symbol\u6709\u4e24\u79cd\u521b\u5efa\u65b9\u5f0f\uff0c\u4e00\u79cd\u662fSymbol()\uff0c\u4e00\u79cd\u662fSymbol.for()\uff1b<\/li>\n<li><strong>\u6ce8\u610f\uff1a<\/strong> Symbol\u4e0d\u662f\u5bf9\u8c61\uff0c\u4e0d\u8981\u4f7f\u7528<code>new<\/code>\u5173\u952e\u5b57\uff1b<\/li>\n<li>Symbol()\u51fd\u6570\u53ef\u63a5\u6536\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e3a\u53c2\u6570\uff0c\u4ec5\u505a\u63cf\u8ff0\u4f7f\u7528\uff0c\u4e0d\u4ee3\u8868Symbol\u7684\u552f\u4e00\u6027\uff1b<\/li>\n<li>Symbol.for()\u63a5\u6536\u4e00\u4e2a\u5b57\u7b26\u4e32\u505a\u4e3a\u53c2\u6570\uff0c\u4ee3\u8868Symbol\u7684\u552f\u4e00\u6027\uff0c\u76f8\u540c\u7684\u5b57\u7b26\u4e32\u80fd\u53d6\u5230\u552f\u4e00\u7684Symbol\uff1b<\/li>\n<\/ul>\n<h2>Symbol\u600e\u4e48\u7528<\/h2>\n<p>\u65e2\u7136Symbol\u7684\u7279\u6027\u4ee3\u8868\u72ec\u4e00\u65e0\u4e8c\uff0c\u90a3\u5178\u578b\u7684\u5e94\u7528\u573a\u666f\u5c31\u662f\u9700\u8981\u552f\u4e00\u6027\u7684\u573a\u6240\uff0c\u6bd4\u5982\u8bf4\u679a\u4e3e\u503c\u3001\u5bf9\u8c61\u7684\u5c5e\u6027\u540d\u6216\u65b9\u6cd5\u540d\u7b49\u3002<br \/>\n* \u4ee5\u6027\u522b\u4e3a\u4f8b\uff0c\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a<code>gender<\/code>\u5bf9\u8c61\uff0c\u5206\u522b\u6709<code>male<\/code>\u548c<code>female<\/code>\u4e24\u4e2a\u5c5e\u6027\uff0c\u6211\u4eec\u5728\u5e94\u7528\u7684\u65f6\u5019\u53ef\u80fd\u53ea\u9700\u8981\u533a\u5206\u8fd9\u4e24\u8005\u4e0d\u4e00\u6837\u5373\u53ef\uff0c\u5e76\u4e0d\u5173\u5fc3\u4ed6\u4eec\u5b9e\u9645\u503c\u662f\u591a\u5c11\uff0c\u53ef\u4ee5\u5c06\u503c\u8bbe\u7f6e\u4e3aSymbol\u7c7b\u578b\u3002<em>(\u5982\u679c\u9700\u8981\u4e0e\u670d\u52a1\u7aef\u4ea4\u4e92\u53e6\u8bf4\uff0c\u8fd9\u91cc\u4e3b\u8981\u662f\u4e3a\u4e86\u8bf4\u660eSymbol\u7684\u7528\u6cd5\uff0c\u52ff\u55b7)<\/em><\/p>\n<pre><code class=\"language-javascript line-numbers\">\/\/ \u6027\u522b:\u5e38\u89c4\u5b9a\u4e49\nconst gender = {\n    male: 'male',   \/\/ \u7537\n    female: 'female'    \/\/ \u5973\n}\n\/\/ \u6027\u522b\uff1aSymbol\u5b9a\u4e49\nconst gender = {\n    male: Symbol(),\n    female: Symbol()\n}\n<\/code><\/pre>\n<ul>\n<li>\u505a\u4e3a\u5bf9\u8c61\u7684\u5c5e\u6027\u540d\u6216\u65b9\u6cd5\u540d\u5728ES6\u4e2d\u4e5f\u5df2\u7ecf\u5f88\u5e38\u89c1\u4e86\uff0c\u53ef\u67e5\u770b\u6570\u7ec4\u7684\u539f\u578b\u5bf9\u8c61\u4e0d\u53ef\u4ee5\u627e\u5230\u540d\u79f0\u4e3aSymbol\u7c7b\u578b\u7684\u65b9\u6cd5\u3002<\/li>\n<li>\u4e3e\u4f8b\u8bf4\u660e\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a<code>person<\/code>\u5bf9\u8c61\uff0c\u6211\u60f3\u6269\u5c55\u4e00\u4e2a\u65b9\u6cd5\uff0c\u4f46\u53c8\u4e0d\u786e\u5b9a\u65b0\u52a0\u7684\u65b9\u6cd5\u540d\u662f\u5426\u4f1a\u5f15\u8d77\u51b2\u7a81\uff0c\u6240\u4ee5\u4f7f\u7528Symbol\u7c7b\u578b\u7684\u503c\u505a\u4e3a\u65b9\u6cd5\u540d<\/li>\n<\/ul>\n<pre><code class=\"language-javascript line-numbers\">\/\/ person\u5bf9\u8c61\nlet person = {\n    name: 'yusian',\n    age: 21,\n    say() {\n        console.log('saying...');\n    }\n}\n\/\/ \u81ea\u5b9a\u4e49\u65b9\u6cd5\u540d\nlet methods = {\n    say: Symbol('say'),\n    run: Symbol('run')\n}\nperson[methods.say] = function () {\n    console.log('symbol saying...');\n}\nperson[methods.run] = function () {\n    console.log('symbol running...');\n}\nperson.say();   \/\/ saying...\nperson[methods.say]();  \/\/ symbol saying...\nperson[methods.run]();  \/\/ symbol running...\n<\/code><\/pre>\n<ul>\n<li>\u4e3a\u4ec0\u4e48\u8981\u8bf4Symbol\u7c7b\u578b\u7684\u65b9\u6cd5\u540d\u5462\uff0c\u6211\u4eec\u4ee5\u540e\u4f1a\u7ecf\u5e38\u4f7f\u7528Symbol\u7c7b\u578b\u7684\u6570\u636e\u505a\u4e3a\u65b9\u6cd5\u540d\u6765\u6269\u5c55\u5bf9\u8c61\u529f\u80fd\u5417\uff1f\u6211\u89c9\u5f97\u4e0d\u5e94\u8be5\u8fd9\u6837\uff0c\u8fd9\u5e76\u4e0d\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u89e3\u51b3\u65b9\u6848\u3002\u6bd4\u8f83\u6709\u4ef7\u503c\u7684\u662fES6\u63d0\u4f9b\u7684\u5185\u7f6e\u7684Symbol\u503c<\/li>\n<li><strong>\u91cd\u70b9\u6765\u4e86<\/strong> \u524d\u9762\u6240\u6709Symbol\u76f8\u5173\u7684\u70b9\u90fd\u4e3a\u8fd9\u4e00\u523b\uff0c\u4ee5<code>Symbol.hasInstance<\/code>\u4e3a\u4f8b\uff0c\u5b9a\u4e49\u4e00\u4e2a<code>Person<\/code>\u7c7b\uff0c\u5728\u7c7b\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\u540d\u4e3a<code>Symbol.hasInstance<\/code>\uff0c\u90a3\u4e48\u5f53\u5bf9\u8be5\u7c7b\u8fdb\u884c<code>instanceof<\/code>\u8c03\u7528\u65f6\u5c31\u4f1a\u6267\u884c\u8be5\u9759\u6001\u65b9\u6cd5\uff0c\u8fd4\u56de\u503c\u5373\u4e3a\u8c03\u7528\u8fd4\u56de\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-javascript line-numbers\">class Person {\n    \/\/ \u65b9\u6cd5\u540d\u4e00\u822c\u4e3a\u5b57\u7b26\u4e32\uff0cSymbol\u7c7b\u578b\u505a\u4e3a\u65b9\u6cd5\u540d\u9700\u8981\u52a0\u65b9\u62ec\u53f7\n    static [Symbol.hasInstance] = function (foo) {\n        console.log('hasInstance, ', foo);\n        return false;\n    }\n}\nlet p = new Person();\nconsole.log(p instanceof Person);   \/\/ false\n<\/code><\/pre>\n<ul>\n<li>\u5927\u81f4\u7528\u6cd5\u5c31\u662f\u8fd9\u4e2a\u6837\u5b50\uff0c\u66f4\u591a\u5185\u7f6eSymbol\u503c\u53c2\u8003\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Symbol\">MDN<\/a><\/li>\n<li>\u6709\u4e86\u8fd9\u4e2a\u57fa\u7840\u540e\u9762\u5c31\u53ef\u4ee5\u8bb2Iterator\u4e86<\/li>\n<\/ul>\n<p>\u53c2\u8003\u94fe\u63a5\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/es6.ruanyifeng.com\/#docs\/symbol\">\u962e\u4e00\u5cf0ES6\u5165\u95e8<\/a><br \/>\n<!--more--><\/p>\n<h1>Iterator<\/h1>\n<p>Iterator\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u5b9e\u73b0\u4e86\u8fd9\u4e2a\u63a5\u53e3\u5c31\u53ef\u8fdb\u884c<code>for...of<\/code>\u904d\u5386\uff0c\u800c\u6240\u8c13\u5b9e\u73b0\u4e86\u8fd9\u4e2a\u63a5\u53e3\u7b80\u5355\u4e00\u70b9\u8bb2\u5c31\u662f\u5bf9\u8c61\u6709<code>Symbol.iterator<\/code>\u5c5e\u6027\u3002\u539f\u751f\u5df2\u5b9e\u73b0\u8be5\u63a5\u53e3\u7684\u6570\u636e\u7c7b\u578b\u6709<code>Array<\/code>\u3001<code>Map<\/code>\u3001<code>Set<\/code>\u3001<code>NodeList<\/code>\u3001<code>String<\/code>\u4ee5\u53ca\u51fd\u6570\u7684<code>arguments<\/code>\u5bf9\u8c61\u3002<br \/>\n* \u4ee5\u6570\u7ec4\u4e3a\u4f8b\uff0c\u4f7f\u7528for&#8230;of\u8fdb\u884c\u904d\u5386\u80fd\u5206\u522b\u8f93\u51fa\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a\u503c\uff1b<\/p>\n<pre><code class=\"language-javascript line-numbers\">let array = [1, 2, 3, 4, 5];\n\/\/ \nfor (let v of array) {\n    console.log(v);\n}\n<\/code><\/pre>\n<ul>\n<li>\u5982\u679c\u662f\u81ea\u5b9a\u4e49\u7684\u4e00\u4e2a\u5bf9\u8c61\uff0c\u60f3\u5b9e\u73b0Iterator\u63a5\u53e3\u8be5\u5982\u4f55\u5904\u7406\u5462\uff1f\u5982\u679c\u6ca1\u6709\u5b9e\u73b0Iterator\u800c\u76f4\u63a5\u8fdb\u884cfor&#8230;of\u8c03\u7528\u4f1a\u629b\u51fa\u5f02\u5e38<code>Uncaught TypeError: object is not iterable<\/code>;<\/li>\n<\/ul>\n<pre><code class=\"language-javascript line-numbers\">let object = {\n    name: 'obj',\n    array: ['a', 'b', 'c', 'd', 'e']\n}\n\/\/ \u62a5\u9519\uff1a`Uncaught TypeError: object is not iterable`\nfor (const iterator of object) {\n    console.log(iterator);\n}\n<\/code><\/pre>\n<ul>\n<li>\u5b9e\u73b0Iterator\u63a5\u53e3\u5c31\u662f\u5728\u5bf9\u8c61\u4e2d\u589e\u52a0\u65b9\u6cd5\u540d\u4e3aSymbol\u7c7b\u578b<code>Symbole.iterator<\/code>\u7684\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5e76\u4e14\u5bf9\u8c61\u8981\u6709\u4e00\u4e2a<code>next<\/code>\u65b9\u6cd5\uff0cnext\u65b9\u6cd5\u53c8\u8981\u8fd4\u56de\u4e00\u4e2a\u5305\u542b<code>value<\/code>(\u4efb\u610f\u7c7b\u578b)\u548c<code>done<\/code>(\u5e03\u5c14\u503c)\u5c5e\u6027\u7684\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-javascript line-numbers\">\/\/ \u7b80\u5355\u6539\u9020\u4e00\u4e0b\nlet object = {\n    name: 'obj',\n    array: ['a', 'b', 'c', 'd', 'e'],\n    [Symbol.iterator]() {\n        return {\n            next: () =&gt; {\n                return {\n                    value: undefined,\n                    done: true\n                }\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<ul>\n<li>\u6b64\u65f6\u518d\u6267\u884c<code>for...of<\/code>\u5c31\u80fd\u5bf9<code>object<\/code>\u8fdb\u884c\u904d\u5386\u64cd\u4f5c\u4e86\uff0c\u4f46\u8fd8\u4e0d\u80fd\u6709\u6b63\u5e38\u8f93\u51fa\uff0c\u56e0\u4e3a<code>next<\/code>\u65b9\u6cd5\u8fd4\u56de\u7684\u5bf9\u8c61\u4e2d<code>done<\/code>\u5c5e\u6027\u51b3\u5b9a\u904d\u5386\u662f\u5426\u7ed3\u675f\uff0c\u5982\u679c\u4e3atrue\u5219\u7ed3\u675f\u904d\u5386\u3002<\/li>\n<li>\u5982\u679c\u5e0c\u671b\u5bf9<code>object<\/code>\u5bf9\u8c61\u8fdb\u884c\u8fed\u4ee3\u8f93\u51fa<code>array<\/code>\u4e2d\u7684\u503c\uff0c\u5219\u53ef\u4ee5\u5982\u4e0b\u6539\u9020\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-javascript line-numbers\">let object = {\n    name: 'obj',\n    array: ['a', 'b', 'c', 'd', 'e'],\n    [Symbol.iterator]() {\n        let i = 0;  \/\/ \u7d22\u5f15\u8bb0\u5f55\n        return {\n            next: () =&gt; {\n                let value = this.array[i];\n                let done = ++i &gt; this.array.length;\n                return {value, done}\n            }\n        }\n    }\n}\nlet iterator = object[Symbol.iterator]();\nconsole.log(iterator.next());   \/\/ { value: \"a\", done: false }\nconsole.log(iterator.next());   \/\/ { value: \"b\", done: false }\nconsole.log(iterator.next());   \/\/ { value: \"c\", done: false }\nconsole.log(iterator.next());   \/\/ { value: \"d\", done: false }\nconsole.log(iterator.next());   \/\/ { value: \"e\", done: false }\nconsole.log(iterator.next());   \/\/ {value: undefined, done: true}\nfor (const iterator of object) {\n    console.log(iterator);  \/\/ \u5206\u522b\u6253\u5370a b c d e\n}\n<\/code><\/pre>\n<p>\u53c2\u8003\u94fe\u63a5\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/es6.ruanyifeng.com\/#docs\/iterator\">\u962e\u4e00\u5cf0ES6\u5165\u95e8<\/a><\/p>\n<h1>Generator<\/h1>\n<h2>Generator\u662f\u4ec0\u4e48<\/h2>\n<p>Generator\u662f\u4ec0\u4e48\uff1f\u6709\u4ec0\u4e48\u4f5c\u7528\uff1f\u4e3a\u4ec0\u4e48\u548c\u8fed\u4ee3\u5668\u53c8\u6709\u5173\u7cfb\uff1f<br \/>\n* \u5148\u8bb2\u4e00\u4e2a\u573a\u666f\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5f88\u957f\u7684\u6570\u7ec4\uff0cN\u591a\u4e2a\u5143\u7d20\uff0c\u6211\u60f3\u4ece\u91cc\u9762\u53d6\u51fa\u51e0\u4e2a\u7528\u4e00\u4e0b\u5e76\u4e14\u53ea\u7528\u4e00\u4e24\u6b21\u3002\u6211\u4eec\u4f20\u7edf\u7684\u65b9\u5f0f\u662f\u5148\u5c06\u6570\u7ec4\u8bfb\u5165\u5185\u5b58\uff0c\u968f\u673a\u8bfb\u53d6\u3002\u53ef\u95ee\u9898\u662f\u4f7f\u7528\u9891\u7387\u4f4e\uff0c\u6570\u7ec4\u5360\u7528\u7a7a\u95f4\u53c8\u5927\u5c82\u4e0d\u662f\u5f88\u6d6a\u8d39\u8d44\u6e90\u3002<br \/>\n* Generator\u5c31\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5b83\u5e76\u4e0d\u4f1a\u9884\u5148\u5c06\u6240\u6709\u6570\u636e\u90fd\u52a0\u8f7d\u8fdb\u6765\uff0c\u800c\u662f\u751f\u6210\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u901a\u8fc7\u8fed\u4ee3\u7684\u65b9\u5f0f\u53d6\u51fa\u9700\u8981\u7684\u6570\u636e\uff0c\u8fd9\u6837\u6ca1\u6709\u5e38\u9a7b\u5185\u5b58\u7684\u95ee\u9898\u4e86\u3002<\/p>\n<p>\u4e5f\u8bb8\u8fd9\u6837\u8bb2\u5e76\u4e0d\u5168\u9762\uff0c\u4f46\u5bf9\u4e8e\u5728\u5b8c\u5168\u4e0d\u6e05\u695aGenerator\u662f\u4ec0\u4e48\u7684\u60c5\u51b5\u4e0b\uff0c\u6709\u4e86\u4e00\u4e2a\u6bd4\u8f83\u76f4\u89c2\u7684\u8ba4\u8bc6\uff0c\u7136\u540e\u518d\u6765\u770b\u770bGenerator\u957f\u4ec0\u4e48\u6837\u5b50\uff1a<br \/>\n* Generator\u4e5f\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u4f46\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5728\u5199\u6cd5\u4e0a\u8981\u52a0\u4e00\u4e2a<code>*<\/code><\/p>\n<pre><code class=\"language-javascript line-numbers\">function *gen() {\n    \/\/ code...\n}\n<\/code><\/pre>\n<ul>\n<li>\u666e\u901a\u51fd\u6570\u8fd4\u56de\u503c\u90fd\u662freturn\u6307\u5b9a\uff0c\u6ca1\u6709return\u8fd4\u56deundefinded\uff0c\u4f46Generator\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668<em>(\u7ec8\u4e8e\u77e5\u9053\u4e3a\u4ec0\u4e48\u8981\u5148\u8bb2\u8fed\u4ee3\u5668\u4e86)<\/em>\uff1b<\/li>\n<li>\u51fd\u6570\u5185\u90e8\u4f7f\u7528yield\u8868\u8fbe\u5f0f\uff0c\u6bcf\u4e2ayield\u5bf9\u5e94\u4e00\u6b21\u8fed\u4ee3\uff0c\u6bcf\u4e2ayield\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u5c31\u662f\u8fed\u4ee3\u7684\u7ed3\u679c\uff0c\u51fd\u6570\u8fd4\u56de\u503c\u662f\u8fed\u4ee3\u7ed3\u675f\u540edone\u4e3atrue\u5bf9\u8c61\u4e2d\u7684value\u503c\uff1b<\/li>\n<\/ul>\n<pre><code class=\"language-javascript line-numbers\">function* gen() {\n    yield 'hello';\n    yield 'world';\n    return 'over!'\n}\nlet iterator = gen();\nconsole.log(iterator.next());   \/\/ {value: \"hello\", done: false}\nconsole.log(iterator.next());   \/\/ {value: \"world\", done: false}\nconsole.log(iterator.next());   \/\/ {value: \"over!\", done: true}\n<\/code><\/pre>\n<ul>\n<li>\u4e3a\u4e86\u65b9\u4fbf\u7406\u89e3\uff0c\u6bcf\u4e00\u6b21<code>yield<\/code>\u53ef\u4ee5\u5f53\u4f5c\u662f\u4e00\u6b21<code>return<\/code>\uff0c\u53ea\u4e0d\u8fc7\u51fd\u6570\u4e0d\u4f1a\u56e0\u6b64\u7ed3\u675f\uff0c\u53ea\u662f\u6682\u505c\u4e86\u800c\u5df2\uff0c\u518d\u6b21\u8fed\u4ee3\u65f6\u4f1a\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c\uff0c\u6bcf\u6b21\u8fed\u4ee3\u5230yield\u5904\u5206\u9694\u3002<\/li>\n<li>\u65e2\u7136\u662f\u8fed\u4ee3\u5668\u90a3\u5c31\u53ef\u4ee5\u7528<code>for...of<\/code>\u8fdb\u884c\u904d\u5386\uff0c<strong>\u6ce8\u610f\uff1a<\/strong> return\u8fd4\u56de\u503c\u4e0d\u4f1a\u88abfor&#8230;of\u904d\u5386\u51fa\u6765\uff0c\u56e0\u4e3afor&#8230;of\u4e0d\u4f1a\u8f93\u51fadone\u4e3atrue\u7684\u5bf9\u8c61\uff1b<\/li>\n<\/ul>\n<pre><code class=\"language-javascript line-numbers\">function* gen() {\n    yield 'hello';\n    yield 'world';\n    return 'Hello World!'\n}\nfor (const v of gen()) {\n    console.log(v);\n}\n<\/code><\/pre>\n<ul>\n<li><code>next<\/code>\u65b9\u6cd5\u53ef\u4ee5\u4f20\u53c2\uff0c\u4f20\u5165\u7684\u53c2\u6570\u4f1a\u5728\u4e0a\u4e00\u4e2ayield\u8868\u8fbe\u5f0f\u5de6\u8fb9\u63a5\u6536\uff0c\u5373\u4e0a\u4e00\u4e2ayield\u8868\u8fbe\u5f0f\u7684\u8fd4\u56de\u503c<em>(\u53ef\u4ee5\u7b80\u5355\u5730\u7406\u89e3\uff0c\u6bcf\u6b21\u8fed\u4ee3\u5305\u542b\u4e86\u4e0a\u4e00\u4e2ayield\u5de6\u8fb9\u7684\u503c\uff0c\u5373next\u7684\u4f20\u53c2)<\/em> \uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<strong>\u7b2c\u4e00\u6b21\u8c03\u7528next\u65b9\u6cd5\u662f\u4e0d\u9700\u8981\u4f20\u53c2\u7684\uff0c\u4f20\u4e86\u4e5f\u6ca1\u7528<\/strong>\uff0c\u56e0\u4e3a\u6ca1\u6709\u201c\u4e0a\u4e00\u4e2ayield\u201d\uff0c\u540c\u6837generator\u51fd\u6570\u4e5f\u53ef\u4ee5\u4f20\u53c2\uff0c\u53ef\u4ee5\u5728\u6240\u6709\u7684yield\u8868\u8fbe\u5f0f\u4e2d\u53ef\u89c1\uff1b<\/li>\n<\/ul>\n<h2>Generator\u600e\u4e48\u7528<\/h2>\n<p>\u65e2\u7136generator\u53ef\u4ee5\u901a\u8fc7\u8fed\u4ee3\u5668\u7684next\u65b9\u6cd5\u8ba9\u51fd\u6570\u6062\u590d\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c\uff0c\u8fd9\u5c31\u7b26\u5408\u5f02\u6b65\u6267\u884c\u7684\u6761\u4ef6\uff0c\u4ee5\u4e00\u4e2a\u793a\u4f8b\u6765\u8bf4\u660e\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">\/\/ \u5b9a\u4e49\u751f\u6210\u5668\u51fd\u6570\nfunction* gen() {\n    let data = yield request('http:\/\/example.com\/xxx');\n    \/\/ \u53ef\u7ee7\u7eed\u5f80\u4e0b\u589e\u52a0\u5f02\u6b65\u64cd\u4f5c\uff0c\u6bcf\u6b21\u5f02\u6b65\u64cd\u4f5c\u7ed3\u675f\u540e\u8c03\u7528\u8fed\u4ee3\u5668\u7684next\u65b9\u6cd5\u5373\u53ef\n    console.log(data);\n}\n\/\/ \u5f02\u6b65\u8bf7\u6c42\u51fd\u6570\nfunction request(url) {\n    fetch(url).then(response =&gt; {\n        return response.json();\n    }).then(data =&gt; {\n        \/\/ \u7b2c\u4e8c\u6b21\u8c03\u7528next\u65b9\u6cd5\uff0c\u8ba9\u8fed\u4ee3\u5668\u7ee7\u7eed\u5f80\u540e\u6267\u884c\n        iterator.next(data);\n    })\n}\n\/\/ \u8fd4\u56de\u8fed\u4ee3\u5668\nlet iterator = gen();\n\/\/ \u7b2c\u4e00\u6b21\u8c03\u7528next\u65b9\u6cd5\uff0c\u542f\u52a8request\u8bf7\u6c42\niterator.next();\n<\/code><\/pre>\n<ul>\n<li>\u9996\u5148\u521b\u5efa\u4e00\u4e2agenerator\uff0c\u5c06\u6240\u6709\u7684\u5f02\u6b65\u64cd\u4f5c\u90fd\u901a\u8fc7yield\u5173\u952e\u5b57\uff0c\u5199\u5230\u51fd\u6570\u4e2d\uff1b<\/li>\n<li>\u8c03\u7528generator\u51fd\u6570\u83b7\u53d6\u8fed\u4ee3\u5668\uff0c\u7b2c\u4e00\u6b21\u8c03\u7528\u8fed\u4ee3\u5668\u7684next\u65b9\u6cd5\u542f\u52a8\u7b2c\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\uff1b<\/li>\n<li>\u6bcf\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\u7ed3\u675f\u540e\u90fd\u8c03\u7528\u8fed\u4ee3\u5668\u7684next\u65b9\u6cd5\u6765\u542f\u52a8\u4e0b\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1\uff0c\u5f02\u6b65\u6267\u884c\u7684\u7ed3\u679c\u53ef\u4ee5\u901a\u8fc7next\u65b9\u6cd5\u4f20\u5230yield\u7684\u8fd4\u56de\u503c\u4e2d\uff1b<\/li>\n<li>\u5982\u6b64\u53cd\u590d\u5373\u53ef\u5b9e\u73b0\u591a\u4e2a\u5f02\u6b65\u64cd\u4f5c\u6309\u6b65\u9aa4\u6267\u884c\uff0c\u800c\u5728\u7a0b\u5e8f\u7f16\u5199\u4e0a\u662f\u591a\u4e2ayield\u6392\u5217\uff0c\u7b80\u5355\u76f4\u89c2\u3002<\/li>\n<\/ul>\n<p>\u53c2\u8003\u94fe\u63a5\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/es6.ruanyifeng.com\/#docs\/generator\">\u962e\u4e00\u5cf0ES6\u5165\u95e8<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u2003\u2003\u672c\u6765\u662f\u60f3\u8bf4\u8bf4\u751f\u6210\u5668Generator\u7684\uff0c\u4f46\u6211\u53d1\u73b0\u548c\u8fed\u4ee3\u6709\u5173\uff0c\u6240\u4ee5\u8981\u5148\u8bb2\u8bb2\u8fed\u4ee3\u5668Iterator\uff0c\u53ef\u8fed\u4ee3\u5668\u53c8\u4f1a\u626f\u5230Symbol\uff0c\u6240\u4ee5\u62c9\u51fa\u6765\u4e00\u8d77\u8bf4\u8bf4\uff1b Symbol Symbol\u662fES6\u5f15\u8fdb\u7684\u4e00\u79cd\u65b0\u6570\u636e\u7c7b\u578b\uff0c\u5b83\u4ee3\u8868\u4e00\u4e2a\u72ec\u4e00\u65e0\u4e8c\u7684\u503c\uff0c\u81f3\u6b64JavaScript\u4e00\u5171\u67097\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5206\u522b\u662f\uff1aUndefinded\u3001String\u3001Symbol\u3001Object\u3001Null\u3001Number\u3001boolean\u3002 Symbol\u662f\u4ec0\u4e48 Symbol\u5230\u5e95\u662f\u4ec0\u4e48\uff1f\u5148\u6765\u4e2a\u6817\u5b50\u770b\u770b\u6548\u679c\uff1a let s1 = Symbol(); let s2 = Symbol(); console.log(s1, typeof s1); \/\/ Symbol() &#8220;symbol&#8221; console.log(s1 === s2); \/\/ false let s3 = Symbol(&#8216;symbol&#8217;); let s4 = Symbol(&#8216;symbol&#8217;); console.log(s3, s4); \/\/ Symbol(symbol) Symbol(symbol) console.log(s3 === s4); \/\/ false let s5 = Symbol.for(&#8216;symbol&#8217;); let s6 = Symbol.for(&#8216;symbol&#8217;); console.log(s5, s6); \/\/ Symbol(symbol) Symbol(symbol) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[230,232,101,53,231],"class_list":["post-1528","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-es6","tag-generator","tag-iterator","tag-javascript","tag-symbol"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1528","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=1528"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1528\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=1528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=1528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=1528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}