{"id":167128,"date":"2024-05-08T15:56:27","date_gmt":"2024-05-08T07:56:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/167128.html"},"modified":"2024-05-08T15:56:31","modified_gmt":"2024-05-08T07:56:31","slug":"javascript%e5%87%bd%e6%95%b0%e5%a3%b0%e6%98%8e%e5%92%8c%e5%87%bd%e6%95%b0%e8%a1%a8%e8%be%be%e5%bc%8f%ef%bc%8c%e5%88%b0%e5%ba%95%e8%af%a5%e7%94%a8%e5%93%aa%e4%b8%80%e4%b8%aa","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/167128.html","title":{"rendered":"JavaScript\u51fd\u6570\u58f0\u660e\u548c\u51fd\u6570\u8868\u8fbe\u5f0f\uff0c\u5230\u5e95\u8be5\u7528\u54ea\u4e00\u4e2a"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27033018\/97c46425-4f22-42b9-8772-b6c3d0ef9dd6.webp\" alt=\"JavaScript\u51fd\u6570\u58f0\u660e\u548c\u51fd\u6570\u8868\u8fbe\u5f0f\uff0c\u5230\u5e95\u8be5\u7528\u54ea\u4e00\u4e2a\" \/><\/p>\n<p><p>\u51fd\u6570\u58f0\u660e\u548c\u51fd\u6570\u8868\u8fbe\u5f0f\u90fd\u662f\u5728JavaScript\u4e2d\u5b9a\u4e49\u51fd\u6570\u7684\u65b9\u6cd5\uff0c\u5b83\u4eec\u5404\u6709\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<strong>\u51fd\u6570\u58f0\u660e<\/strong>\u63d0\u4f9b\u4e86\u51fd\u6570\u63d0\u5347\uff08hoisting\uff09\u7684\u7279\u6027\uff0c\u610f\u5473\u7740\u4f60\u5728\u5b9a\u4e49\u51fd\u6570\u524d\u5c31\u53ef\u4ee5\u8c03\u7528\u5b83\uff0c\u8fd9\u6709\u52a9\u4e8e\u7ec4\u7ec7\u548c\u7406\u89e3\u4ee3\u7801\u7ed3\u6784\u3002\u800c<strong>\u51fd\u6570\u8868\u8fbe\u5f0f<\/strong>\uff0c\u5305\u62ec\u533f\u540d\u51fd\u6570\u6216\u5177\u540d\u51fd\u6570\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u4e0d\u4f1a\u88ab\u63d0\u5347\uff0c\u56e0\u6b64\u5fc5\u987b\u5728\u5b9a\u4e49\u4e4b\u540e\u624d\u80fd\u8c03\u7528\u3002\u9009\u62e9\u4f7f\u7528\u54ea\u4e00\u4e2a\u901a\u5e38\u53d6\u51b3\u4e8e\u51fd\u6570\u8c03\u7528\u7684\u65f6\u673a\u3001\u4ee3\u7801\u7ed3\u6784\u7684\u6e05\u6670\u5ea6\u4ee5\u53ca\u4e2a\u4eba\u504f\u597d\u3002<\/p>\n<\/p>\n<p><p>\u51fd\u6570\u58f0\u660e\u63d0\u4f9b\u4e86\u4ee3\u7801\u7ec4\u7ec7\u4e0a\u7684\u7075\u6d3b\u6027\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u5f53\u4e00\u4e2a\u51fd\u6570\u9700\u8981\u5728\u4ee3\u7801\u903b\u8f91\u7684\u591a\u4e2a\u5730\u65b9\u4f7f\u7528\u65f6\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u4e00\u4e2a\u6e05\u6670\u7684\u58f0\u660e\u544a\u8bc9\u8bfb\u8005\u8fd9\u4e2a\u51fd\u6570\u662f\u5e72\u4ec0\u4e48\u7684\uff0c\u800c\u4e0d\u5fc5\u62c5\u5fc3\u8c03\u7528\u987a\u5e8f\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u51fd\u6570\u58f0\u660e\u7684\u7279\u70b9<\/h2>\n<\/p>\n<p><p><strong>\u51fd\u6570\u58f0\u660e<\/strong>\u662f\u4e00\u79cd\u6700\u5e38\u89c1\u7684\u51fd\u6570\u5b9a\u4e49\u65b9\u5f0f\uff0c\u5b83\u4f7f\u7528<code>function<\/code>\u5173\u952e\u5b57\u540e\u8ddf\u4e00\u4e2a\u51fd\u6570\u540d\u79f0\u548c\u51fd\u6570\u4f53\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u51fd\u6570\u63d0\u5347<\/strong><\/p>\n<p>\u51fd\u6570\u58f0\u660e\u7684\u6700\u5927\u4f18\u52bf\u662f\u5176\u4f1a\u88ab\u63d0\u5347\u5230\u4ee3\u7801\u5757\u7684\u9876\u90e8\u3002\u8fd9\u610f\u5473\u7740\u5373\u4fbf\u51fd\u6570\u58f0\u660e\u5728\u4ee3\u7801\u7684\u540e\u9762\u90e8\u5206\uff0c\u4f60\u4ecd\u53ef\u4ee5\u5728\u58f0\u660e\u4e4b\u524d\u8c03\u7528\u5b83\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u6613\u8bfb\u6027<\/strong><\/p>\n<p>\u51fd\u6570\u58f0\u660e\u901a\u5e38\u66f4\u6613\u4e8e\u7406\u89e3\uff0c\u56e0\u4e3a\u5b83\u4eec\u5728\u4ee3\u7801\u7ed3\u6784\u4e2d\u7684\u4f4d\u7f6e\u901a\u5e38\u8868\u660e\u4e86\u4ee3\u7801\u7684\u903b\u8f91\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u51fd\u6570\u8868\u8fbe\u5f0f\u7684\u7279\u70b9<\/strong><\/p>\n<\/p>\n<p><p>\u51fd\u6570\u8868\u8fbe\u5f0f\u4e0d\u540c\uff0c\u5b83\u53ef\u4ee5\u662f\u533f\u540d\u7684\u6216\u8005\u662f\u547d\u540d\u7684\uff0c\u4e14\u5fc5\u987b\u5728\u5b9a\u4e49\u4e4b\u540e\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u53ef\u4ee5\u662f\u533f\u540d\u7684<\/strong><\/p>\n<p>\u51fd\u6570\u8868\u8fbe\u5f0f\u53ef\u4ee5\u662f\u533f\u540d\u7684\uff0c\u5373\u6ca1\u6709\u7ed9\u5b9a\u51fd\u6570\u540d\u3002\u8fd9\u4f7f\u5f97\u5b83\u4eec\u975e\u5e38\u9002\u5408\u7528\u4f5c\u7acb\u5373\u6267\u884c\u7684\u51fd\u6570\uff08IIFE\uff09\u6216\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\u4f5c\u4e3a\u56de\u8c03\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u53d8\u91cf\u4f5c\u7528\u57df<\/strong><\/p>\n<p>\u4f7f\u7528\u51fd\u6570\u8868\u8fbe\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5229\u7528\u95ed\u5305\u548c\u4f5c\u7528\u57df\u7684\u7279\u6027\u6765\u63a7\u5236\u51fd\u6570\u7684\u53ef\u89c1\u6027\u548c\u751f\u547d\u5468\u671f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e09\u3001\u5e94\u7528\u573a\u666f<\/strong><\/p>\n<\/p>\n<p><p>\u9009\u62e9\u4f55\u65f6\u4f7f\u7528\u51fd\u6570\u58f0\u660e\u4e0e\u51fd\u6570\u8868\u8fbe\u5f0f\u901a\u5e38\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u4e8b\u4ef6\u5904\u7406\u548c\u56de\u8c03<\/strong><\/p>\n<p>\u51fd\u6570\u8868\u8fbe\u5f0f\u7279\u522b\u9002\u5408\u7528\u4f5c\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u548c\u56de\u8c03\u51fd\u6570\u3002\u8fd9\u662f\u56e0\u4e3a\u8fd9\u4e9b\u51fd\u6570\u901a\u5e38\u5728\u7279\u5b9a\u7684\u65f6\u95f4\u70b9\u88ab\u8c03\u7528\uff0c\u4e0d\u9700\u8981\u63d0\u524d\u5728\u4ee3\u7801\u4e2d\u663e\u793a\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u6761\u4ef6\u51fd\u6570\u5b9a\u4e49<\/strong><\/p>\n<p>\u5f53\u4f60\u6839\u636e\u7279\u5b9a\u7684\u6761\u4ef6\u5b9a\u4e49\u4e0d\u540c\u7684\u51fd\u6570\u5b9e\u73b0\u65f6\uff0c\u51fd\u6570\u8868\u8fbe\u5f0f\u662f\u4e0d\u9519\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\u903b\u8f91\u5e76\u4f7f\u5176\u66f4\u52a0\u6e05\u6670\u3002<\/p>\n<\/p>\n<p><p><strong>\u56db\u3001\u9009\u62e9\u4f9d\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u867d\u7136\u51fd\u6570\u58f0\u660e\u548c\u51fd\u6570\u8868\u8fbe\u5f0f\u5728\u8bed\u6cd5\u4e0a\u6709\u6240\u4e0d\u540c\uff0c\u4f46\u4e24\u8005\u901a\u5e38\u53ef\u4ee5\u4e92\u6362\u4f7f\u7528\u3002\u4f60\u7684\u9009\u62e9\u53ef\u80fd\u53d6\u51b3\u4e8e\u4ee3\u7801\u7ec4\u7ec7\u3001\u9879\u76ee\u89c4\u8303\u4ee5\u53ca\u4e2a\u4eba\u504f\u597d\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u4ee3\u7801\u7ec4\u7ec7<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u559c\u6b22\u4e00\u773c\u5c31\u80fd\u770b\u5230\u6240\u6709\u7684\u51fd\u6570\u5b9a\u4e49\uff0c\u53ef\u80fd\u4f1a\u503e\u5411\u4e8e\u4f7f\u7528\u51fd\u6570\u58f0\u660e\u3002\u5982\u679c\u4f60\u66f4\u5173\u6ce8\u51fd\u6570\u7684\u4f7f\u7528\u4e0a\u4e0b\u6587\uff0c\u53ef\u80fd\u4f1a\u559c\u6b22\u51fd\u6570\u8868\u8fbe\u5f0f\u7684\u7d27\u51d1\u548c\u76f4\u63a5\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u4e2a\u4eba\u504f\u597d<\/strong><\/p>\n<p>\u4e0d\u540c\u7684JavaScript\u5f00\u53d1\u8005\u5bf9\u4e8e\u8fd9\u4e24\u79cd\u65b9\u5f0f\u6709\u4e0d\u540c\u7684\u504f\u597d\u3002\u4e00\u4e9b\u5f00\u53d1\u8005\u4e60\u60ef\u4e8e\u4f7f\u7528\u51fd\u6570\u58f0\u660e\uff0c\u800c\u53e6\u4e00\u4e9b\u5219\u503e\u5411\u4e8e\u51fd\u6570\u8868\u8fbe\u5f0f\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7684\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u6839\u636e\u4e0d\u540c\u7684\u7f16\u7a0b\u98ce\u683c\u548c\u9700\u6c42\uff0c\u6df7\u5408\u4f7f\u7528\u51fd\u6570\u58f0\u660e\u548c\u51fd\u6570\u8868\u8fbe\u5f0f\u3002\u7406\u89e3\u5b83\u4eec\u5404\u81ea\u7684\u4f18\u52bf\u548c\u5c40\u9650\u6027\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u52a0\u6070\u5f53\u5730\u5728\u5de5\u4f5c\u4e2d\u4f5c\u51fa\u9009\u62e9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4ec0\u4e48\u662fJavaScript\u51fd\u6570\u58f0\u660e\u548c\u51fd\u6570\u8868\u8fbe\u5f0f\uff1f\u4e24\u8005\u6709\u4f55\u4e0d\u540c\u4e4b\u5904\uff1f<\/strong><\/p>\n<p>JavaScript\u51fd\u6570\u58f0\u660e\u662f\u901a\u8fc7\u5173\u952e\u5b57<code>function<\/code>\u6765\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u4efb\u4f55\u4f4d\u7f6e\u5b9a\u4e49\uff0c\u5e76\u4e14\u63d0\u5347\u5230\u6240\u5728\u4f5c\u7528\u57df\u7684\u9876\u90e8\u3002\u800c\u51fd\u6570\u8868\u8fbe\u5f0f\u662f\u5c06\u51fd\u6570\u8d4b\u503c\u7ed9\u53d8\u91cf\u6216\u8005\u5e38\u91cf\u7684\u65b9\u5f0f\u6765\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u901a\u5e38\u51fa\u73b0\u5728\u8d4b\u503c\u8bed\u53e5\u6216\u8005\u56de\u8c03\u51fd\u6570\u4e2d\u3002<\/p>\n<p><strong>2. \u5728\u9009\u62e9\u4f7f\u7528JavaScript\u51fd\u6570\u58f0\u660e\u8fd8\u662f\u51fd\u6570\u8868\u8fbe\u5f0f\u65f6\uff0c\u6709\u54ea\u4e9b\u56e0\u7d20\u9700\u8981\u8003\u8651\uff1f<\/strong><\/p>\n<p>\u9009\u62e9\u4f7f\u7528\u51fd\u6570\u58f0\u660e\u8fd8\u662f\u51fd\u6570\u8868\u8fbe\u5f0f\u4e3b\u8981\u53d6\u51b3\u4e8e\u4ee3\u7801\u7ed3\u6784\u548c\u4e0a\u4e0b\u6587\u73af\u5883\u3002\u5982\u679c\u9700\u8981\u5728\u5b9a\u4e49\u51fd\u6570\u4e4b\u524d\u8c03\u7528\u51fd\u6570\uff0c\u90a3\u4e48\u51fd\u6570\u58f0\u660e\u662f\u4e00\u4e2a\u597d\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u7684\u4efb\u4f55\u4f4d\u7f6e\u88ab\u8c03\u7528\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u5982\u679c\u9700\u8981\u5728\u5c06\u51fd\u6570\u8d4b\u503c\u7ed9\u53d8\u91cf\u4e4b\u524d\u8fdb\u884c\u4e00\u4e9b\u903b\u8f91\u5904\u7406\uff0c\u90a3\u4e48\u51fd\u6570\u8868\u8fbe\u5f0f\u66f4\u4e3a\u5408\u9002\u3002<\/p>\n<p><strong>3. \u54ea\u4e00\u4e2a\u66f4\u597d\uff1fJavaScript\u51fd\u6570\u58f0\u660e\u8fd8\u662f\u51fd\u6570\u8868\u8fbe\u5f0f\uff1f<\/strong><\/p>\n<p>\u6ca1\u6709\u7edd\u5bf9\u7684\u7b54\u6848\u3002\u9009\u62e9\u4f7f\u7528\u51fd\u6570\u58f0\u660e\u8fd8\u662f\u51fd\u6570\u8868\u8fbe\u5f0f\u7684\u6700\u4f73\u65b9\u5f0f\u662f\u6839\u636e\u5177\u4f53\u60c5\u51b5\u6765\u51b3\u5b9a\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u7b80\u5355\u7684\u51fd\u6570\uff0c\u51fd\u6570\u58f0\u660e\u53ef\u80fd\u66f4\u6613\u8bfb\u548c\u65b9\u4fbf\u3002\u7136\u800c\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u51fd\u6570\u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u9009\u62e9\uff0c\u5e76\u53ef\u4ee5\u66f4\u597d\u5730\u9002\u5e94\u590d\u6742\u7684\u4ee3\u7801\u7ed3\u6784\u3002\u6700\u91cd\u8981\u7684\u662f\uff0c\u5728\u9879\u76ee\u4e2d\u4fdd\u6301\u4e00\u81f4\u6027\uff0c\u4ee5\u4fbf\u4ee3\u7801\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u51fd\u6570\u58f0\u660e\u548c\u51fd\u6570\u8868\u8fbe\u5f0f\u90fd\u662f\u5728JavaScript\u4e2d\u5b9a\u4e49\u51fd\u6570\u7684\u65b9\u6cd5\uff0c\u5b83\u4eec\u5404\u6709\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u51fd\u6570\u58f0\u660e\u63d0\u4f9b\u4e86\u51fd\u6570\u63d0\u5347 [&hellip;]","protected":false},"author":3,"featured_media":167135,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/167128"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=167128"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/167128\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/167135"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=167128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=167128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=167128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}