{"id":32322,"date":"2024-11-25T09:39:43","date_gmt":"2024-11-25T01:39:43","guid":{"rendered":"https:\/\/fwq.ai\/blog\/32322\/"},"modified":"2024-11-25T09:39:43","modified_gmt":"2024-11-25T01:39:43","slug":"%e6%8e%8c%e6%8f%a1-javascript%ef%bc%9a%e7%ac%ac-3-%e9%83%a8%e5%88%86%ef%bc%8c%e6%8e%a2%e7%b4%a2%e5%be%aa%e7%8e%af","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/32322\/","title":{"rendered":"\u638c\u63e1 JavaScript\uff1a\u7b2c 3 \u90e8\u5206\uff0c\u63a2\u7d22\u5faa\u73af"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169323343162315.jpg\" class=\"aligncenter\" title=\"\u638c\u63e1 JavaScript\uff1a\u7b2c 3 \u90e8\u5206\uff0c\u63a2\u7d22\u5faa\u73af\u63d2\u56fe\" alt=\"\u638c\u63e1 JavaScript\uff1a\u7b2c 3 \u90e8\u5206\uff0c\u63a2\u7d22\u5faa\u73af\u63d2\u56fe\" \/><\/p>\n<p>\u5047\u8bbe\u60a8\u7684\u4efb\u52a1\u662f\u7f16\u5199\u4e00\u4e2a\u663e\u793a\u6570\u5b57 1 \u5230 100 \u7684\u7a0b\u5e8f\u3002\u5b8c\u6210\u6b64\u4efb\u52a1\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u7f16\u5199 100 \u6761 console.log() \u8bed\u53e5\u3002\u4f46\u6211\u786e\u4fe1\u4f60\u4e0d\u4f1a\uff0c\u56e0\u4e3a\u4f60\u4f1a\u538c\u5026\u7b2c\u4e5d\u884c\u6216\u7b2c\u5341\u884c\u3002<\/p>\n<p>\u6bcf\u4e2a\u8bed\u53e5\u4e2d\u552f\u4e00\u6539\u53d8\u7684\u90e8\u5206\u662f\u6570\u5b57\uff0c\u56e0\u6b64\u5e94\u8be5\u6709\u4e00\u79cd\u65b9\u6cd5\u53ea\u7f16\u5199\u4e00\u4e2a\u8bed\u53e5\u3002\u8fd8\u6709\u5faa\u73af\u3002\u5faa\u73af\u8ba9\u6211\u4eec\u5728\u4ee3\u7801\u5757\u4e2d\u91cd\u590d\u6267\u884c\u4e00\u7ec4\u6b65\u9aa4\u3002<\/p>\n<ul>\n<li><span>While \u5faa\u73af<\/span><\/li>\n<li><span>Do-While \u5faa\u73af<\/span><\/li>\n<li><span>For \u5faa\u73af<\/span><\/li>\n<li><span>\u6570\u7ec4<\/span><\/li>\n<li><span>For-Of \u5faa\u73af<\/span><\/li>\n<li><span>For-In \u5faa\u73af<\/span><\/li>\n<\/ul>\n<h2>While \u5faa\u73af<\/h2>\n<p>\u5f53\u67d0\u4e9b\u6761\u4ef6\u8ba1\u7b97\u4e3a true \u65f6\uff0cWhile \u5faa\u73af\u5c06\u91cd\u590d\u6267\u884c\u4e00\u7ec4\u8bed\u53e5\u3002\u5f53\u6761\u4ef6\u4e3a\u5047\u65f6\uff0c\u7a0b\u5e8f\u5c06\u9000\u51fa\u5faa\u73af\u3002\u8fd9\u79cd\u5faa\u73af\u5728\u6267\u884c\u8fed\u4ee3\u4e4b\u524d\u6d4b\u8bd5\u6761\u4ef6\u3002\u8fed\u4ee3\u662f\u5faa\u73af\u4f53\u7684\u6267\u884c\u3002\u8fd9\u662f while \u5faa\u73af\u7684\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<pre>let x = 10;\n\nwhile(x &amp;gt; 0) {\n   console.log(`x is now ${x}`);\n   x -= 1;\n}\n\nconsole.log(\"Out of the loop.\");\n\n\/* Outputs:\nx is now 10\nx is now 9\nx is now 8\nx is now 7\nx is now 6\nx is now 5\nx is now 4\nx is now 3\nx is now 2\nx is now 1\nOut of the loop. *\/\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06 x \u8bbe\u7f6e\u4e3a 10\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6761\u4ef6 x &gt; 0 \u7684\u8ba1\u7b97\u7ed3\u679c\u4e3a true\uff0c\u56e0\u6b64\u6267\u884c\u5757\u5185\u7684\u4ee3\u7801\u3002\u8fd9\u4f1a\u6253\u5370\u8bed\u53e5\u201cx is now 10\u201d\uff0c\u5e76\u5c06 x \u7684\u503c\u51cf 1\u3002\u5728\u4e0b\u4e00\u6b21\u68c0\u67e5\u671f\u95f4\uff0cx \u7b49\u4e8e 9\uff0c\u4ecd\u7136\u5927\u4e8e 0\u3002\u56e0\u6b64\u5faa\u73af\u7ee7\u7eed\u3002\u5728\u6700\u540e\u4e00\u6b21\u8fed\u4ee3\u4e2d\uff0cx \u6700\u7ec8\u53d8\u4e3a 1\uff0c\u5e76\u4e14\u6211\u4eec\u6253\u5370\u201cx is now 1\u201d\u3002\u4e4b\u540e\uff0cx \u53d8\u4e3a 0\uff0c\u56e0\u6b64\u6211\u4eec\u6b63\u5728\u8bc4\u4f30\u7684\u6761\u4ef6\u4e0d\u518d\u6210\u7acb true\u3002\u7136\u540e\uff0c\u6211\u4eec\u5f00\u59cb\u6267\u884c\u5faa\u73af\u5916\u7684\u8bed\u53e5\u5e76\u6253\u5370\u201cOut of theloop\u201d\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u8fd9\u662f while \u5faa\u73af\u7684\u4e00\u822c\u5f62\u5f0f\uff1a<\/p>\n<pre>while (condition) {\n    statement;\n    statement;\n    etc.\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f7f\u7528 while \u5faa\u73af\u65f6\u8981\u8bb0\u4f4f\u7684\u4e00\u4ef6\u4e8b\u662f\u4e0d\u8981\u521b\u5efa\u6c38\u65e0\u6b62\u5883\u7684\u5faa\u73af\u3002\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\u662f\u56e0\u4e3a\u6761\u4ef6\u6c38\u8fdc\u4e0d\u4f1a\u53d8\u4e3a false\u3002\u5982\u679c\u5b83\u53d1\u751f\u5728\u4f60\u8eab\u4e0a\uff0c\u4f60\u7684\u7a0b\u5e8f\u5c31\u4f1a\u5d29\u6e83\u3002\u8fd9\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre>let x = 10;\n\nwhile(x &amp;gt; 0) {\n   console.log(`x is now ${x}`);\n   x += 1;\n}\n\nconsole.log(\"Out of the loop.\");\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u589e\u52a0 x \u800c\u4e0d\u662f\u51cf\u5c11\uff0c\u5e76\u4e14 x \u7684\u503c\u5df2\u7ecf\u5927\u4e8e 0\uff0c\u56e0\u6b64\u5faa\u73af\u5c06\u65e0\u9650\u671f\u5730\u7ee7\u7eed\u4e0b\u53bb\u3002<\/p>\n<h3>\u4efb\u52a1<\/h3>\n<p>\u8fd9\u4e2a\u5faa\u73af\u4f53\u4f1a\u88ab\u6267\u884c\u591a\u5c11\u6b21\uff1f<\/p>\n<pre>let i = 0;\n\nwhile (i \n&lt;h2 id=\"toc-u4gh-do-while-loops\"&gt;Do-While \u5faa\u73af&lt;\/h2&gt;\n&lt;p&gt;do-while \u5faa\u73af\u5c06\u9996\u5148\u6267\u884c\u8bed\u53e5\u4e3b\u4f53\uff0c\u7136\u540e\u68c0\u67e5\u6761\u4ef6\u3002\u5f53\u60a8\u77e5\u9053\u8981\u81f3\u5c11\u8fd0\u884c\u4ee3\u7801\u4e00\u6b21\u65f6\uff0c\u8fd9\u79cd\u5faa\u73af\u5f88\u6709\u7528\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c06\u8bb0\u5f55 x \u7684\u503c\u4e00\u6b21\uff0c\u5373\u4f7f\u6761\u4ef6\u8ba1\u7b97\u7ed3\u679c\u4e3a false\uff0c\u56e0\u4e3a x \u7b49\u4e8e 0\u3002&lt;\/p&gt;\n&lt;pre class=\"brush:javascript;toolbal:false;\"&gt;let x = 0;\n\ndo {\n   console.log(`x is now ${x}`);\n   x -= 1;\n} while(x &amp;gt; 0);\n\nconsole.log(\"Out of the loop.\");\n\n\/* Outputs:\nx is now 0\nOut of the loop. *\/\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6211\u5728\u81ea\u5df1\u7684\u9879\u76ee\u4e2d\u591a\u6b21\u4f7f\u7528 do-while \u5faa\u73af\u6765\u751f\u6210\u968f\u673a\u503c\uff0c\u7136\u540e\u53ea\u8981\u5b83\u4eec\u4e0d\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u5c31\u7ee7\u7eed\u751f\u6210\u5b83\u4eec\u3002\u8fd9\u6709\u52a9\u4e8e\u907f\u514d\u7531\u4e8e\u521d\u59cb\u5316\u548c\u5faa\u73af\u5185\u91cd\u65b0\u5206\u914d\u800c\u5bfc\u81f4\u7684\u91cd\u590d\u3002<\/p>\n<p>\u8fd9\u662f do-while \u5faa\u73af\u7684\u4e00\u822c\u5f62\u5f0f\uff1a<\/p>\n<pre>do {\n    statement;\n    statement;\n    etc.\n} while (condition);\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3>\u4efb\u52a1<\/h3>\n<p>\u7f16\u5199\u4e00\u4e2a do-while \u5faa\u73af\u6765\u663e\u793a\u6570\u5b57 1 \u5230 10\u3002<\/p>\n<h2>For \u5faa\u73af<\/h2>\n<p>for \u5faa\u73af\u5c06\u91cd\u590d\u6267\u884c\u4ee3\u7801\u5757\u7279\u5b9a\u6b21\u6570\u3002\u4ee5\u4e0b\u793a\u4f8b\u663e\u793a\u6570\u5b57 1 \u5230 10\uff1a<\/p>\n<pre>for (let i = 1; i \n&lt;p&gt;\u8fd9\u662f for \u5faa\u73af\u7684\u4e00\u822c\u5f62\u5f0f\uff1a&lt;\/p&gt;\n&lt;pre class=\"brush:plaintext;toolbal:false;\"&gt;for (initial; condition; step) {\n    statement;\n    statement;\n    etc.\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>Initial<\/strong> \u662f\u4e00\u4e2a\u8bbe\u7f6e\u53d8\u91cf\u503c\u7684\u8868\u8fbe\u5f0f\u3002\u8fd9\u662f\u6267\u884c\u521d\u59cb\u5316\u7684\u53ef\u9009\u8868\u8fbe\u5f0f\u3002<\/p>\n<p><strong>\u6761\u4ef6<\/strong>\u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u5fc5\u987b\u4e3a\u771f\u624d\u80fd\u6267\u884c\u8bed\u53e5\u3002\u4ec5\u5f53\u6761\u4ef6\u8ba1\u7b97\u7ed3\u679c\u4e3a true \u65f6\u624d\u4f1a\u6267\u884c\u5757\u5185\u7684\u8bed\u53e5\u3002\u5b8c\u5168\u8df3\u8fc7\u6761\u4ef6\u5c06\u5bfc\u81f4\u5b83\u4eec\u59cb\u7ec8\u4e3a\u771f\uff0c\u56e0\u6b64\u60a8\u5fc5\u987b\u4ee5\u5176\u4ed6\u65b9\u5f0f\u9000\u51fa\u5faa\u73af\u3002<\/p>\n<p><strong>step<\/strong> \u662f\u4e00\u4e2a\u9012\u589e\u53d8\u91cf\u503c\u7684\u8868\u8fbe\u5f0f\u3002\u8fd9\u4e5f\u662f\u53ef\u9009\u7684\uff0c\u5e76\u4e14\u5728 for \u5757\u5185\u7684\u6240\u6709\u8bed\u53e5\u6267\u884c\u5b8c\u6bd5\u540e\u6267\u884c\u3002\u6b65\u9aa4\u8868\u8fbe\u5f0f\u901a\u5e38\u7528\u4e8e\u63a5\u8fd1\u5faa\u73af\u7684\u7ed3\u675f\u6761\u4ef6\u3002<\/p>\n<p>\u60a8\u8fd8\u53ef\u4ee5\u5c06 for \u5faa\u73af\u7f16\u5199\u4e3a\u7b49\u6548\u7684 while \u5faa\u73af\u3002\u60a8\u6240\u9700\u8981\u505a\u7684\u5c31\u662f\u7a0d\u5fae\u6539\u53d8\u60a8\u7684\u9648\u8ff0\u548c\u6761\u4ef6\u3002\u4e0a\u9762\u7684 for \u5faa\u73af\u53ef\u4ee5\u91cd\u5199\u4e3a\uff1a<\/p>\n<pre>initial;\n\nwhile(condition) {\n    statement;\n    statement;\n    etc.\n    step;\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e00\u79cd\u7f16\u7a0b\u6a21\u5f0f\u662f\u4f7f\u7528 for \u5faa\u73af\u7528\u53d8\u91cf\u672c\u8eab\u548c\u65b0\u503c\u6765\u66f4\u65b0\u53d8\u91cf\u7684\u503c\u3002\u6b64\u793a\u4f8b\u5c06\u6570\u5b57 1 \u5230 10 \u76f8\u52a0\uff1a<\/p>\n<pre>let x = 0;\n\nfor (let i = 1; i \n&lt;p&gt;\u8fd9\u662f\u7b49\u6548\u7684 while \u5faa\u73af\uff0c\u5b83\u7ed9\u51fa\u76f8\u540c\u7684\u8f93\u51fa\uff1a&lt;\/p&gt;\n&lt;pre class=\"brush:javascript;toolbal:false;\"&gt;let x = 0;\nlet i = 1;\n\nwhile(i \n&lt;p&gt;\u60a8\u5e94\u8be5\u6ce8\u610f\u6211\u662f\u5982\u4f55\u5728 while \u5757\u7684\u672b\u5c3e\u800c\u4e0d\u662f\u5728\u5f00\u5934\u8fdb\u884c\u589e\u91cf\u7684\u3002\u5728\u5f00\u59cb\u65f6\u589e\u52a0\u5faa\u73af\u53d8\u91cf i \u4f1a\u7ed9\u6211\u4eec 65\uff0c\u8fd9\u4e0d\u662f\u6211\u4eec\u5728\u8fd9\u91cc\u6253\u7b97\u505a\u7684\u3002&lt;\/p&gt;\n&lt;p&gt;+= \u8fd0\u7b97\u7b26\u662f\u4e00\u4e2a\u8d4b\u503c\u8fd0\u7b97\u7b26\uff0c\u5b83\u5c06\u503c\u6dfb\u52a0\u56de\u53d8\u91cf\u3002\u8fd9\u662f\u6240\u6709\u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u5217\u8868\uff1a&lt;\/p&gt;\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<table>\n<thead>\n<tr>\n<th>\u64cd\u4f5c\u5458<\/th>\n<th>\u793a\u4f8b<\/th>\n<th>\u7b49\u6548<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>+=<\/td>\n<td>x += 2<\/td>\n<td>&nbsp;x = x + 2<\/td>\n<\/tr>\n<tr>\n<td>-=<\/td>\n<td>x -= 2<\/td>\n<td>x = x &#8211; 2<\/td>\n<\/tr>\n<tr>\n<td>*=<\/td>\n<td>x *= 2<\/td>\n<td>x = x * 2<\/td>\n<\/tr>\n<tr>\n<td>\/=<\/td>\n<td>x \/= 2<\/td>\n<td>x = x \/ 2<\/td>\n<\/tr>\n<tr>\n<td>%=<\/td>\n<td>x%=2<\/td>\n<td>x = x % 2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u4efb\u52a1<\/h3>\n<p>\u7f16\u5199\u4e00\u4e2a for \u5faa\u73af\u6765\u8ba1\u7b97\u6570\u5b57\u7684\u9636\u4e58\u3002\u6570\u5b57<em>n<\/em>\u7684\u56e0\u5b50\u662f\u4ece1\u5230n\u7684\u6240\u6709\u6574\u6570\u7684\u4e58\u79ef\u3002\u4f8b\u5982\uff0c4\uff01 \uff084 \u9636\u4e58\uff09\u4e3a 1 x 2 x 3 x 4\uff0c\u7b49\u4e8e 24\u3002<\/p>\n<h2>\u6570\u7ec4<\/h2>\n<p>\u6570\u7ec4\u662f\u4e00\u4e2a\u5305\u542b\u9879\u76ee\u5217\u8868\u7684\u5bf9\u8c61\uff0c\u8fd9\u4e9b\u9879\u76ee\u79f0\u4e3a\u5143\u7d20\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8fdb\u884c\u8bbf\u95ee\u3002\u7d22\u5f15\u662f\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e\u3002\u7b2c\u4e00\u4e2a\u5143\u7d20\u4f4d\u4e8e\u7d22\u5f15 0 \u5904\u3002<\/p>\n<p>\u6570\u7ec4\u6709\u4e00\u4e2a\u540d\u4e3a length \u7684\u5c5e\u6027\uff0c\u5b83\u4e3a\u60a8\u63d0\u4f9b\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u603b\u6570\u3002\u8fd9\u610f\u5473\u7740\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a for \u5faa\u73af\u6765\u8fed\u4ee3\u6570\u7ec4\u4e2d\u7684\u9879\u76ee\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>let arr = [1, 2, \"Hello\", \"World\"];\n\nfor (let i = 0; i \n&lt;p&gt;\u4e8c\u7ef4\u6570\u7ec4\u662f\u6307\u5143\u7d20\u4e3a\u6570\u7ec4\u7684\u6570\u7ec4\u3002\u4f8b\u5982\uff1a&lt;\/p&gt;\n&lt;pre class=\"brush:javascript;toolbal:false;\"&gt;let arr = [\n    [1, 2],\n    [\"Hello\", \"World\"]\n];\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u662f\u5faa\u73af\u6570\u7ec4\u5e76\u663e\u793a\u6bcf\u4e2a\u5143\u7d20\u7684\u65b9\u5f0f\uff1a<\/p>\n<pre>for (let i = 0; i \n&lt;p&gt;\u60a8\u5c06\u5982\u4f55\u91cd\u5199\u4e0a\u9762\u7684\u5faa\u73af\uff0c\u4ee5\u4fbf\u4ece\u672b\u5c3e\u5f00\u59cb\u6253\u5370\u6570\u7ec4\u5143\u7d20\uff1f&lt;\/p&gt;\n&lt;h2 id=\"toc-uxf5-for-of-loop\"&gt;For-Of \u5faa\u73af&lt;\/h2&gt;\n&lt;p&gt;\u8fed\u4ee3\u6570\u7ec4\u65f6\u6700\u5e38\u89c1\u7684\u573a\u666f\u4e4b\u4e00\u662f\u4ece\u5934\u5f00\u59cb\uff0c\u7136\u540e\u4e00\u6b21\u904d\u5386\u6240\u6709\u5143\u7d20\uff0c\u76f4\u5230\u5230\u8fbe\u672b\u5c3e\u3002\u6709\u4e00\u79cd\u66f4\u77ed\u7684\u65b9\u6cd5\u53ef\u4ee5\u5c06 for \u5faa\u73af\u7f16\u5199\u4e3a for-of \u5faa\u200b\u200b\u73af\u3002&lt;\/p&gt;\n&lt;p&gt;for-of \u5faa\u200b\u200b\u73af\u8ba9\u6211\u4eec\u53ef\u4ee5\u5faa\u73af\u904d\u5386\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u4f8b\u5982\u6570\u7ec4\u3001\u6620\u5c04\u548c\u5b57\u7b26\u4e32\uff09\u7684\u503c\u3002 for-of \u5faa\u200b\u200b\u73af\u57fa\u672c\u4e0a\u7528\u4e8e\u8fed\u4ee3\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\u3002\u8fd9\u662f\u4e0a\u4e00\u8282\u4e2d\u7684\u5faa\u73af\uff0c\u91cd\u5199\u4e3a for-of \u5faa\u200b\u200b\u73af\u3002&lt;\/p&gt;\n&lt;pre class=\"brush:javascript;toolbal:false;\"&gt;let arr = [1, 2, \"Hello\", \"World\"];\n\nfor (let item of arr) {\n    console.log(item);\n}\n\n\/*\nOutputs:\n1\n2\nHello\nWorld\n*\/\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5faa\u73af\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre>let big_word = \"Pulchritudinous\";\n\nfor (let char of big_word) {\n    console.log(char);\n}\n\n\/*\nOutputs:\nP\nu\nl\nc\nh\nr\ni\nt\nu\nd\ni\nn\no\nu\ns\n*\/\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2>For-In \u5faa\u73af<\/h2>\n<p>\u8fd9\u79cd\u5faa\u73af\u8ba9\u6211\u4eec\u53ef\u4ee5\u5faa\u73af\u8bbf\u95ee\u5bf9\u8c61\u7684\u5c5e\u6027\u3002\u5bf9\u8c61\u662f\u4e00\u79cd\u5c06\u952e\u6620\u5c04\u5230\u503c\u7684\u6570\u636e\u7ed3\u6784\u3002 JavaScript \u4e2d\u7684\u6570\u7ec4\u4e5f\u662f\u5bf9\u8c61\uff0c\u56e0\u6b64\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528 for-in \u5faa\u73af\u6765\u5faa\u73af\u6570\u7ec4\u5c5e\u6027\u3002\u6211\u4eec\u9996\u5148\u770b\u770b\u5982\u4f55\u4f7f\u7528 for-in \u5faa\u73af\u6765\u8fed\u4ee3\u5bf9\u8c61\u952e\u6216\u5c5e\u6027\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528 for-in \u5faa\u73af\u904d\u5386\u5bf9\u8c61\u952e\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>let obj = {\n    foo: \"Hello\",\n    bar: \"World\"\n};\n\nfor (let key in obj) {\n    console.log(key);\n}\n\n\/*\nOutputs:\nfoo\nbar\n*\/\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0b\u9762\u662f\u4f7f\u7528 for-in \u5faa\u73af\u904d\u5386\u6570\u7ec4\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>let arr = [1, 2, \"hello\", \"world\"];\n\nfor (let key in arr) {\n    console.log(arr[key]);\n}\n\n\/* Outputs:\n1\n2\nhello\nworld *\/\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6211\u60f3\u8865\u5145\u4e00\u70b9\uff0c\u5373\u4f7f\u6211\u4eec\u80fd\u591f\u4f7f\u7528 for-in \u5faa\u73af\u904d\u5386\u6570\u7ec4\u5143\u7d20\uff0c\u60a8\u4e5f\u5e94\u8be5\u907f\u514d\u8fd9\u6837\u505a\u3002\u8fd9\u662f\u56e0\u4e3a\u5b83\u7684\u76ee\u7684\u662f\u5faa\u73af\u8bbf\u95ee\u5bf9\u8c61\u7684\u5c5e\u6027\uff0c\u5982\u679c\u60a8\u53ea\u60f3\u5faa\u73af\u6570\u7ec4\u7d22\u5f15\u6765\u83b7\u53d6\u6570\u7ec4\u503c\uff0c\u5219\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u5f97\u5230\u610f\u5916\u7684\u7ed3\u679c\u3002<\/p>\n<h2>\u8bc4\u8bba<\/h2>\n<p>\u5faa\u73af\u8ba9\u6211\u4eec\u51cf\u5c11\u4ee3\u7801\u4e2d\u7684\u91cd\u590d\u3002 While \u5faa\u73af\u8ba9\u6211\u4eec\u91cd\u590d\u4e00\u4e2a\u52a8\u4f5c\uff0c\u76f4\u5230\u6761\u4ef6\u4e3a\u5047\u3002 do-while \u5faa\u73af\u5c06\u81f3\u5c11\u6267\u884c\u4e00\u6b21\u3002 For \u5faa\u73af\u8ba9\u6211\u4eec\u91cd\u590d\u4e00\u4e2a\u52a8\u4f5c\uff0c\u76f4\u5230\u5230\u8fbe\u8ba1\u6570\u7ed3\u675f\u3002 for-in \u5faa\u73af\u7684\u8bbe\u8ba1\u662f\u4e3a\u4e86\u8ba9\u6211\u4eec\u53ef\u4ee5\u8bbf\u95ee\u5bf9\u8c61\u4e2d\u7684\u952e\u3002 for-of \u5faa\u200b\u200b\u73af\u7684\u8bbe\u8ba1\u662f\u4e3a\u4e86\u8ba9\u6211\u4eec\u80fd\u591f\u83b7\u53d6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u503c\u3002 <\/p>\n<p>\u5728\u4e0b\u4e00\u90e8\u5206\u4e2d\uff0c\u60a8\u5c06\u5b66\u4e60\u51fd\u6570\u3002<\/p>\n<p><em>\u672c\u6587\u5df2\u6839\u636e Monty Shokeen \u7684\u8d21\u732e\u8fdb\u884c\u4e86\u66f4\u65b0\u3002 Monty \u662f\u4e00\u4f4d\u5168\u6808\u5f00\u53d1\u4eba\u5458\uff0c\u4ed6\u4e5f\u559c\u6b22\u7f16\u5199\u6559\u7a0b\u548c\u5b66\u4e60\u65b0\u7684 JavaScript \u5e93\u3002<\/em><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u638c\u63e1 JavaScript\uff1a\u7b2c 3 \u90e8\u5206\uff0c\u63a2\u7d22\u5faa\u73af\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>\u5047\u8bbe\u60a8\u7684\u4efb\u52a1\u662f\u7f16\u5199\u4e00\u4e2a\u663e\u793a\u6570\u5b57 1 \u5230 100 \u7684\u7a0b\u5e8f\u3002\u5b8c\u6210\u6b64\u4efb\u52a1\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u7f16\u5199 100 \u6761 console.log() \u8bed\u53e5\u3002\u4f46\u6211\u786e\u4fe1\u4f60\u4e0d\u4f1a\uff0c\u56e0\u4e3a\u4f60\u4f1a\u538c\u5026\u7b2c\u4e5d\u884c\u6216\u7b2c\u5341\u884c\u3002 \u6bcf\u4e2a\u8bed\u53e5\u4e2d\u552f\u4e00\u6539\u53d8\u7684\u90e8\u5206\u662f\u6570\u5b57\uff0c\u56e0\u6b64\u5e94\u8be5\u6709\u4e00\u79cd\u65b9\u6cd5\u53ea\u7f16\u5199\u4e00\u4e2a\u8bed\u53e5\u3002\u8fd8\u6709\u5faa\u73af\u3002\u5faa\u73af\u8ba9\u6211\u4eec\u5728\u4ee3\u7801\u5757\u4e2d\u91cd\u590d\u6267\u884c\u4e00\u7ec4\u6b65\u9aa4\u3002 While \u5faa\u73af Do-While \u5faa\u73af For \u5faa\u73af \u6570\u7ec4 For-Of \u5faa\u73af For-In \u5faa\u73af While \u5faa\u73af \u5f53\u67d0\u4e9b\u6761\u4ef6\u8ba1\u7b97\u4e3a true \u65f6\uff0cWhile \u5faa\u73af\u5c06\u91cd\u590d\u6267\u884c\u4e00\u7ec4\u8bed\u53e5\u3002\u5f53\u6761\u4ef6\u4e3a\u5047\u65f6\uff0c\u7a0b\u5e8f\u5c06\u9000\u51fa\u5faa\u73af\u3002\u8fd9\u79cd\u5faa\u73af\u5728\u6267\u884c\u8fed\u4ee3\u4e4b\u524d\u6d4b\u8bd5\u6761\u4ef6\u3002\u8fed\u4ee3\u662f\u5faa\u73af\u4f53\u7684\u6267\u884c\u3002\u8fd9\u662f while \u5faa\u73af\u7684\u57fa\u672c\u793a\u4f8b\uff1a let x = 10; while(x &amp;gt; 0) { console.log(`x is now ${x}`); x -= 1; } console.log(&#8220;Out of the loop.&#8221;); \/* Outputs: x is now 10 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-32322","post","type-post","status-publish","format-standard","hentry","category-cms"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32322","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=32322"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32322\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=32322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=32322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=32322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}