{"id":2036,"date":"2024-11-10T13:28:52","date_gmt":"2024-11-10T05:28:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2036\/"},"modified":"2024-11-10T13:28:52","modified_gmt":"2024-11-10T05:28:52","slug":"%e5%8f%af%e8%bf%ad%e4%bb%a3-javascript-%e6%8c%91%e6%88%98","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2036\/","title":{"rendered":"\u53ef\u8fed\u4ee3 &#8211; JavaScript \u6311\u6218"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173063250495863.jpg\" class=\"aligncenter\" title=\"\u53ef\u8fed\u4ee3 &#8211; JavaScript \u6311\u6218\u63d2\u56fe\" alt=\"\u53ef\u8fed\u4ee3 &#8211; JavaScript \u6311\u6218\u63d2\u56fe\" \/><\/p>\n<p>\u60a8\u53ef\u4ee5\u5728 hub \u4ed3\u5e93\u4e2d\u627e\u5230\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u7684\u6240\u6709\u4ee3\u7801\u3002<\/p>\n<hr>\n<h2> \u53ef\u8fed\u4ee3\u76f8\u5173\u7684\u6311\u6218 <\/h2>\n<hr>\n<h3> \u53ef\u8fed\u4ee3 <\/h3>\n<pre>\/**\n * @param {any} data\n * @return {object}\n *\/\n\nfunction createCustomIterable(data) {\n  return {\n    [Symbol.iterator]() {\n      let index = 0;\n      return {\n        next() {\n          if (index &lt; data.length) {\n            return {\n              value: data[index++],\n              done: false,\n            };\n          } else {\n            return {\n              value: undefined,\n              done: true,\n            };\n          }\n        },\n      };\n    },\n  };\n}\n\n\/\/ Usage example:\nconst customIterable = createCustomIterable([1, 2, 3, 4]);\n\n\/\/ Using for...of loop\nfor (const item of customIterable) {\n  console.log(item);\n}\n\/**\n * 1\n * 2\n * 3\n * 4\n *\/\n\n\/\/ Using spread operator\nconst arrayFromIterable = [...customIterable];\nconsole.log(arrayFromIterable); \/\/ =&gt; [1, 2, 3, 4]\n\n\/\/ Using Array.from()\nconst anotherArray = Array.from(customIterable);\nconsole.log(anotherArray); \/\/ =&gt; [1, 2, 3, 4]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h2> \u53c2\u8003 <\/h2>\n<ul>\n<li>\u8fed\u4ee3\u5668 &#8211; mdn<\/li>\n<li>\u8fed\u4ee3\u534f\u8bae &#8211; mdn<\/li>\n<li>[iterator.prototypesymbol.iterator &#8211; mdn](https:\/\/developer.mozilla.org\/en-us\/docs\/web\/javascript\/reference\/global_objects\/iterator\/symbol.iterator)<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u53ef\u8fed\u4ee3 &#8211; JavaScript \u6311\u6218\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u60a8\u53ef\u4ee5\u5728 hub \u4ed3\u5e93\u4e2d\u627e\u5230\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u7684\u6240\u6709\u4ee3\u7801\u3002 \u53ef\u8fed\u4ee3\u76f8\u5173\u7684\u6311\u6218 \u53ef\u8fed\u4ee3 \/** * @param {any} data * @return {object} *\/ function createCustomIterable(data) { return { [Symbol.iterator]() { let index = 0; return { next() { if (index &lt; data.length) { return { value: data[index++], done: false, }; } else { return { value: undefined, done: true, }; } }, }; }, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-2036","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2036","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=2036"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2036\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}