{"id":18,"date":"2007-05-21T00:03:08","date_gmt":"2007-05-21T04:03:08","guid":{"rendered":"http:\/\/prglab.com\/blog\/?page_id=18"},"modified":"2007-08-25T13:49:35","modified_gmt":"2007-08-25T17:49:35","slug":"js-forin","status":"publish","type":"page","link":"https:\/\/www.prglab.com\/blog\/js-tutorial\/js-forin","title":{"rendered":"JavaScript \u7684For&#8230;In \u8bed\u53e5"},"content":{"rendered":"<p>for&#8230;in \u8bed\u53e5\u662f\u7528\u6765\u5faa\u73af\u8bbf\u95ee\u4e00\u4e2a\u6570\u7ec4\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u6216\u4e00\u4e2a\u5bf9\u8c61\u7684\u5404\u4e2a\u5c5e\u6027\u503c\u7684\u3002<\/p>\n<p>for &#8230; in \u5faa\u73af\u4f53\u4e2d\u7684\u4ee3\u7801\u5bf9\u6bcf\u4e00\u4e2a\u5143\u7d20\u6216\u5c5e\u6027\u6267\u884c\u4e00\u6b21\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">for (variable in object)<br \/>\n{<br \/>\n\u8981\u6267\u884c\u7684\u4ee3\u7801<br \/>\n}<\/td>\n<\/tr>\n<\/table>\n<p>\u53c2\u6570 variable \u53ef\u4ee5\u662f\u4e00\u4e2a\u53d8\u91cf\u540d\uff0c \u4e00\u4e2a\u6570\u7ec4\u5143\u7d20\uff0c\u6216\u4e00\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u3002<\/p>\n<p><strong>\u4f8b\u5b50<\/strong><br \/>\n\u7528for&#8230;in \u6765\u5faa\u73af\u8bbf\u95ee\u4e00\u4e2a\u6570\u7ec4\uff1a<\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">&lt;html&gt;<br \/>\n&lt;body&gt;&lt;script type=&#8221;text\/javascript&#8221;&gt;<br \/>\nvar x<br \/>\nvar mycars = new Array()<br \/>\nmycars[0] = &#8220;Saab&#8221;<br \/>\nmycars[1] = &#8220;Volvo&#8221;<br \/>\nmycars[2] = &#8220;BMW&#8221;<\/p>\n<p>for (x in mycars)<br \/>\n{<br \/>\ndocument.write(mycars[x] + &#8220;&lt;br \/&gt;&#8221;)<br \/>\n}<br \/>\n&lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/td>\n<\/tr>\n<\/table>\n<p><strong>\u7ed3\u679c<\/strong><\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">Saab<br \/>\nVolvo<br \/>\nBMW<\/td>\n<\/tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>for&#8230;in \u8bed\u53e5\u662f\u7528\u6765\u5faa\u73af\u8bbf\u95ee\u4e00\u4e2a\u6570\u7ec4\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u6216\u4e00\u4e2a\u5bf9\u8c61\u7684\u5404\u4e2a\u5c5e\u6027\u503c\u7684\u3002 for &#8230; in \u5faa\u73af\u4f53\u4e2d\u7684\u4ee3\u7801\u5bf9\u6bcf\u4e00\u4e2a\u5143\u7d20\u6216\u5c5e\u6027\u6267\u884c\u4e00\u6b21\u3002 \u8bed\u6cd5 for (variable in object) { \u8981\u6267\u884c\u7684\u4ee3\u7801 } \u53c2\u6570 variable \u53ef\u4ee5\u662f\u4e00\u4e2a\u53d8\u91cf\u540d\uff0c \u4e00\u4e2a\u6570\u7ec4\u5143\u7d20\uff0c\u6216\u4e00\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u3002 \u4f8b\u5b50 \u7528for&#8230;in \u6765\u5faa\u73af\u8bbf\u95ee\u4e00\u4e2a\u6570\u7ec4\uff1a &lt;html&gt; &lt;body&gt;&lt;script type=&#8221;text\/javascript&#8221;&gt; var x var mycars = new Array() mycars[0] = &#8220;Saab&#8221; mycars[1] = &#8220;Volvo&#8221; mycars[2] = &#8220;BMW&#8221; for (x in mycars) { document.write(mycars[x] + &#8220;&lt;br \/&gt;&#8221;) } &lt;\/script&gt; &lt;\/body&gt; &lt;\/html&gt; \u7ed3\u679c Saab [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3,"menu_order":11,"comment_status":"open","ping_status":"open","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/18"}],"collection":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":0,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/18\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/3"}],"wp:attachment":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/media?parent=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}