{"id":568,"date":"2025-12-07T09:52:57","date_gmt":"2025-12-07T01:52:57","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=568"},"modified":"2025-12-07T09:52:57","modified_gmt":"2025-12-07T01:52:57","slug":"javascript%e4%b8%ad%e5%a6%82%e4%bd%95%e9%81%8d%e5%8e%86%e5%af%b9%e8%b1%a1%e6%96%b9%e6%b3%95%e6%b1%87%e6%80%bb","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/07\/javascript%e4%b8%ad%e5%a6%82%e4%bd%95%e9%81%8d%e5%8e%86%e5%af%b9%e8%b1%a1%e6%96%b9%e6%b3%95%e6%b1%87%e6%80%bb\/","title":{"rendered":"JavaScript\u4e2d\u5982\u4f55\u904d\u5386\u5bf9\u8c61(\u65b9\u6cd5\u6c47\u603b)"},"content":{"rendered":"\n<p>\u4e0b\u9762\u6574\u7406\u4e00\u4efd <strong>\u300aJavaScript \u4e2d\u904d\u5386\u5bf9\u8c61\u7684\u6240\u6709\u5e38\u7528\u65b9\u6cd5\uff08\u6700\u5168\u6c47\u603b\u7248\uff09\u300b<\/strong>\uff0c\u8986\u76d6 ES5 \u2192 ES6+ \u2192 \u9ad8\u7ea7\u6280\u5de7\uff0c\u8ba9\u4f60\u5728\u4efb\u4f55\u573a\u666f\u90fd\u80fd\u9009\u5bf9\u65b9\u6cd5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\ude80 <strong>JavaScript\u904d\u5386\u5bf9\u8c61\u7684\u6240\u6709\u65b9\u6cd5\uff08\u6700\u5168\u603b\u7ed3\uff09<\/strong><\/h1>\n\n\n\n<p>\u5bf9\u8c61\u904d\u5386 \u2b07 \u53ef\u4ee5\u5206\u4e3a <strong>\u952e\uff08key\uff09\u904d\u5386<\/strong>\u3001<strong>\u503c\uff08value\uff09\u904d\u5386<\/strong>\u3001<strong>[key,value] \u7ec4\u5408\u904d\u5386<\/strong>\u3001<strong>\u53ef\u679a\u4e3e\/\u4e0d\u53ef\u679a\u4e3e\/\u539f\u578b\u94fe\u904d\u5386<\/strong> \u7b49\u573a\u666f\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u9010\u6761\u5217\u51fa\u793a\u4f8b\u4e0e\u9002\u7528\u8303\u56f4\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 1. <strong>for&#8230;in\uff08\u53ef\u904d\u5386\u539f\u578b\u94fe\uff09<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst obj = { a: 1, b: 2 };\nfor (let key in obj) {\n  console.log(key, obj&#x5B;key]);\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u2714 \u7279\u70b9<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u904d\u5386 <strong>\u53ef\u679a\u4e3e\u5c5e\u6027<\/strong><\/li>\n\n\n\n<li><strong>\u5305\u542b\u539f\u578b\u94fe\u4e0a\u7684\u5c5e\u6027<\/strong><\/li>\n\n\n\n<li>\u9700\u8981 <code>hasOwnProperty<\/code> \u8fc7\u6ee4<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfor (let key in obj) {\n  if (obj.hasOwnProperty(key)) {\n    console.log(key, obj&#x5B;key]);\n  }\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 2. <strong>Object.keys()\uff08\u53ea\u904d\u5386\u81ea\u8eab\u53ef\u679a\u4e3e\u5c5e\u6027\uff09<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nObject.keys(obj).forEach(key =&gt; {\n  console.log(key, obj&#x5B;key]);\n});\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u2714 \u7279\u70b9<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd4\u56de <strong>\u81ea\u8eab\u53ef\u679a\u4e3e\u5c5e\u6027<\/strong> \u7684\u6570\u7ec4<\/li>\n\n\n\n<li>\u4e0d\u5305\u542b Symbol \u548c\u539f\u578b\u94fe<\/li>\n\n\n\n<li>\u4f7f\u7528\u9891\u7387\u6700\u9ad8<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 3. <strong>Object.values()\uff08\u904d\u5386\u503c\uff09<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nObject.values(obj).forEach(v =&gt; {\n  console.log(v);\n});\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u2714 \u7279\u70b9<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ea\u904d\u5386\u5bf9\u8c61\u7684\u503c<\/li>\n\n\n\n<li>\u4e0d\u5305\u62ec key<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 4. <strong>Object.entries()\uff08\u952e\u503c\u5bf9\u904d\u5386\uff09<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfor (let &#x5B;key, value] of Object.entries(obj)) {\n  console.log(key, value);\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u2714 \u7279\u70b9<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u952e\u503c\u5bf9\u5f62\u5f0f<\/li>\n\n\n\n<li>\u5e38\u7528\u4e8e\u8f6c\u6362 Map\u3001\u6784\u5efa\u8868\u683c\u3001\u5e8f\u5217\u5316\u7b49<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 5. <strong>Object.getOwnPropertyNames()\uff08\u5305\u62ec\u4e0d\u53ef\u679a\u4e3e\u5c5e\u6027\uff09<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nObject.getOwnPropertyNames(obj).forEach(key =&gt; {\n  console.log(key, obj&#x5B;key]);\n});\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u2714 \u7279\u70b9<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5305\u62ec\uff1a\u53ef\u679a\u4e3e + \u4e0d\u53ef\u679a\u4e3e<\/li>\n\n\n\n<li>\u4e0d\u5305\u62ec Symbol<\/li>\n<\/ul>\n\n\n\n<p>\u9002\u5408\u9700\u8981\u83b7\u53d6 <strong>\u9690\u85cf\u5c5e\u6027<\/strong> \u65f6\u4f7f\u7528\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 6. <strong>Object.getOwnPropertySymbols()\uff08\u904d\u5386 Symbol \u5c5e\u6027\uff09<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nObject.getOwnPropertySymbols(obj).forEach(key =&gt; {\n  console.log(key, obj&#x5B;key]);\n});\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u2714 \u7279\u70b9<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e13\u95e8\u7528\u4e8e <strong>Symbol \u952e<\/strong><\/li>\n\n\n\n<li>\u4e0d\u5305\u542b\u666e\u901a\u952e<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 7. <strong>Reflect.ownKeys()\uff08\u6700\u5168\u5c5e\u6027\u904d\u5386\uff09<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nReflect.ownKeys(obj).forEach(key =&gt; {\n  console.log(key, obj&#x5B;key]);\n});\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u2714 \u7279\u70b9<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5305\u542b\u6240\u6709\u5c5e\u6027\uff1a\n<ul class=\"wp-block-list\">\n<li>\u53ef\u679a\u4e3e<\/li>\n\n\n\n<li>\u4e0d\u53ef\u679a\u4e3e<\/li>\n\n\n\n<li>Symbol<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u6700\u5f3a\u5927\u7684\u5bf9\u8c61\u904d\u5386\u65b9\u6cd5<\/strong><\/li>\n<\/ul>\n\n\n\n<p>\u9002\u5408\u4f5c\uff1a\u5e8f\u5217\u5316\u3001\u6846\u67b6\u5e95\u5c42\u3001\u4ee3\u7406\u7b49\u9ad8\u7ea7\u529f\u80fd<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 8. <strong>Object.getPrototypeOf() + \u904d\u5386\u539f\u578b\u94fe<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nlet current = obj;\nwhile (current) {\n  console.log(&quot;\u539f\u578b\uff1a&quot;, current);\n  current = Object.getPrototypeOf(current);\n}\n\n<\/pre><\/div>\n\n\n<p>\u7528\u4e8e\u8c03\u8bd5\u3001\u624b\u5199\u7ee7\u627f\u3001\u5e8f\u5217\u5316\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 9. <strong>for&#8230;of\uff08\u7528\u4e8e Map\u3001Object.entries \u7b49\u95f4\u63a5\u904d\u5386\uff09<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfor (let &#x5B;key, value] of Object.entries(obj)) {\n  console.log(key, value);\n}\n\n<\/pre><\/div>\n\n\n<p><code>for...of<\/code> \u672c\u8eab\u4e0d\u80fd\u904d\u5386\u666e\u901a\u5bf9\u8c61\uff08\u9664\u975e\u4f60\u5b9e\u73b0 iterator\uff09\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 10. <strong>\u4f7f\u7528 Map\/WeakMap \u904d\u5386\u5bf9\u8c61\u7ed3\u6784<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst map = new Map(Object.entries(obj));\nmap.forEach((v, k) =&gt; console.log(k, v));\n\n<\/pre><\/div>\n\n\n<p>\u7528\u4e8e\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u53ef\u904d\u5386\u7ed3\u6784\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 11. <strong>\u9012\u5f52\u904d\u5386\u5d4c\u5957\u5bf9\u8c61\uff08\u6df1\u5ea6\u904d\u5386\uff09<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfunction deepTraverse(o) {\n  for (let k in o) {\n    if (o.hasOwnProperty(k)) {\n      console.log(k, o&#x5B;k]);\n      if (typeof o&#x5B;k] === &#039;object&#039; &amp;amp;&amp;amp; o&#x5B;k] !== null) {\n        deepTraverse(o&#x5B;k]);\n      }\n    }\n  }\n}\n\n<\/pre><\/div>\n\n\n<p>\u7528\u4e8e\u914d\u7f6e\u6587\u4ef6\u3001\u6811\u7ed3\u6784\u3001JSON \u7b49\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udccc <strong>\u5404\u65b9\u6cd5\u5dee\u5f02\u5bf9\u6bd4\u8868<\/strong><\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6cd5<\/th><th>\u662f\u5426\u904d\u5386\u539f\u578b\u94fe<\/th><th>\u662f\u5426\u53ef\u679a\u4e3e<\/th><th>\u662f\u5426\u542b Symbol<\/th><th>\u662f\u5426\u542b\u4e0d\u53ef\u679a\u4e3e<\/th><\/tr><\/thead><tbody><tr><td>for&#8230;in<\/td><td>\u2714<\/td><td>\u2714<\/td><td>\u2716<\/td><td>\u2716<\/td><\/tr><tr><td>Object.keys<\/td><td>\u2716<\/td><td>\u2714<\/td><td>\u2716<\/td><td>\u2716<\/td><\/tr><tr><td>Object.values<\/td><td>\u2716<\/td><td>\u2714<\/td><td>\u2716<\/td><td>\u2716<\/td><\/tr><tr><td>Object.entries<\/td><td>\u2716<\/td><td>\u2714<\/td><td>\u2716<\/td><td>\u2716<\/td><\/tr><tr><td>Object.getOwnPropertyNames<\/td><td>\u2716<\/td><td>\u2714 + \u2716<\/td><td>\u2716<\/td><td>\u2714<\/td><\/tr><tr><td>Object.getOwnPropertySymbols<\/td><td>\u2716<\/td><td>\u2714 + \u2716<\/td><td>\u2714<\/td><td>\u2714<\/td><\/tr><tr><td>Reflect.ownKeys<\/td><td>\u2716<\/td><td>\u2714 + \u2716<\/td><td>\u2714<\/td><td>\u2714<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\ud83d\udc49 \u7ed3\u8bba\uff1a<br><strong>\u8981\u904d\u5386\u6240\u6709\u5c5e\u6027\uff0c\u7528 Reflect.ownKeys()<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83c\udfaf \u5b9e\u9645\u9879\u76ee\u5982\u4f55\u9009\u62e9\uff1f<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">\u2714 \u6700\u5e38\u7528\uff08\u63a8\u8350\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nObject.keys(obj).forEach(...)\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u2714 \u5f53\u4f60\u9700\u8981\u952e + \u503c\uff08\u63a8\u8350\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfor (const &#x5B;k, v] of Object.entries(obj)) {}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u2714 \u9700\u8981\u904d\u5386 Symbol<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nObject.getOwnPropertySymbols()\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u2714 \u9700\u8981\u5168\u90e8\u5c5e\u6027\uff08\u6700\u5b8c\u6574\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nReflect.ownKeys()\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u2714 \u8001\u4ee3\u7801\u517c\u5bb9<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfor...in + hasOwnProperty\n\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u6574\u7406\u4e00\u4efd \u300aJavaScript \u4e2d\u904d\u5386\u5bf9\u8c61\u7684\u6240\u6709\u5e38\u7528\u65b9\u6cd5\uff08\u6700\u5168\u6c47\u603b\u7248\uff09\u300b\uff0c&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/07\/javascript%e4%b8%ad%e5%a6%82%e4%bd%95%e9%81%8d%e5%8e%86%e5%af%b9%e8%b1%a1%e6%96%b9%e6%b3%95%e6%b1%87%e6%80%bb\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-568","post","type-post","status-publish","format-standard","hentry","category-javascript"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/568","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=568"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/568\/revisions"}],"predecessor-version":[{"id":569,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/568\/revisions\/569"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}