{"id":315162,"date":"2024-05-20T23:53:07","date_gmt":"2024-05-20T15:53:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/315162.html"},"modified":"2024-05-20T23:53:17","modified_gmt":"2024-05-20T15:53:17","slug":"javascript-%e5%87%bd%e6%95%b0%e7%9b%b8%e5%85%b3%e7%9f%a5%e8%af%86%e7%82%b9%e6%9c%89%e5%93%aa%e4%ba%9b-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/315162.html","title":{"rendered":"JavaScript \u51fd\u6570\u76f8\u5173\u77e5\u8bc6\u70b9\u6709\u54ea\u4e9b"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27034846\/46b3e34b-eb1e-47bc-9377-189af4710f10.webp\" alt=\"JavaScript \u51fd\u6570\u76f8\u5173\u77e5\u8bc6\u70b9\u6709\u54ea\u4e9b\" \/><\/p>\n<p><p>\u51fd\u6570\u662fJavaScript\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u57fa\u7840\u6784\u4ef6\u4e4b\u4e00\uff0c\u5b83\u4eec\u7528\u4e8e\u5c01\u88c5\u4e00\u6bb5\u53ef\u4ee5\u5728\u591a\u5904\u6267\u884c\u7684\u4ee3\u7801\u3002<strong>\u51fd\u6570\u7684\u5de5\u4f5c\u539f\u7406\u3001\u51fd\u6570\u7c7b\u578b\u3001\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u3001\u51fd\u6570\u4f5c\u7528\u57df\u548c\u95ed\u5305\u3001\u4ee5\u53ca\u9ad8\u7ea7\u51fd\u6570<\/strong>\u662fJavaScript\u4e2d\u51fd\u6570\u76f8\u5173\u7684\u5173\u952e\u77e5\u8bc6\u70b9\u3002\u5728\u8fd9\u4e9b\u77e5\u8bc6\u70b9\u4e2d\uff0c<strong>\u51fd\u6570\u7684\u5de5\u4f5c\u539f\u7406<\/strong>\u5c24\u5176\u57fa\u7840\u4e14\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u6d89\u53ca\u5230\u5982\u4f55\u521b\u5efa\u51fd\u6570\u3001\u51fd\u6570\u662f\u5982\u4f55\u6267\u884c\u7684\u3001\u4ee5\u53ca\u51fd\u6570\u5728JavaScript\u5f15\u64ce\u4e2d\u7684\u8868\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u6df1\u5165\u4e86\u89e3<strong>\u51fd\u6570\u7684\u5de5\u4f5c\u539f\u7406<\/strong>\u3002\u51fd\u6570\u5728JavaScript\u4e2d\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u58f0\u660e\u3001\u51fd\u6570\u8868\u8fbe\u5f0f\u6216\u7bad\u5934\u51fd\u6570\u7b49\u65b9\u5f0f\u6765\u521b\u5efa\u3002\u51fd\u6570\u58f0\u660e\u662f\u4e00\u79cd\u63d0\u524d\u5728\u4ee3\u7801\u6267\u884c\u524d\u89e3\u6790\u7684\u65b9\u5f0f\uff0c\u4ece\u800c\u4f7f\u5f97\u51fd\u6570\u80fd\u591f\u5728\u58f0\u660e\u4e4b\u524d\u88ab\u8c03\u7528\uff1b\u800c\u51fd\u6570\u8868\u8fbe\u5f0f\u5219\u5fc5\u987b\u5728\u5b9a\u4e49\u540e\u624d\u80fd\u88ab\u8c03\u7528\u3002\u7bad\u5934\u51fd\u6570\u5219\u662f\u4e00\u79cd\u66f4\u52a0\u7b80\u6d01\u7684\u51fd\u6570\u8868\u8fbe\u65b9\u5f0f\uff0c\u5b83\u4e0d\u4ec5\u8bed\u6cd5\u7b80\u6d01\uff0c\u800c\u4e14\u4e0d\u7ed1\u5b9a\u81ea\u8eab\u7684<code>this<\/code>\u503c\u3002\u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u4e00\u4e2a\u65b0\u7684\u6267\u884c\u4e0a\u4e0b\u6587\u5c31\u4f1a\u88ab\u521b\u5efa\uff0c\u51fd\u6570\u5185\u7684\u53d8\u91cf\u548c\u51fd\u6570\u4f1a\u5728\u8fd9\u4e2a\u4e0a\u4e0b\u6587\u4e2d\u8fdb\u884c\u89e3\u6790\u548c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u51fd\u6570\u7684\u521b\u5efa\u548c\u7c7b\u578b<\/h3>\n<\/p>\n<p><p><strong>\u521b\u5efa\u51fd\u6570<\/strong>\u7684\u65b9\u5f0f\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u51fd\u6570\u58f0\u660e\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-javascript\">function hello() {<\/p>\n<p>  return &#039;Hello, World!&#039;;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u51fd\u6570\u8868\u8fbe\u5f0f\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-javascript\">const greet = function() {<\/p>\n<p>  return &#039;Greetings!&#039;;<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u7bad\u5934\u51fd\u6570\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-javascript\">const helloArrow = () =&gt; &#039;Hello, Arrow!&#039;;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u4e0d\u540c\u7684\u51fd\u6570\u521b\u5efa\u65b9\u5f0f\u51b3\u5b9a\u4e86\u51fd\u6570\u7684\u4f5c\u7528\u57df\u3001\u63d0\u5347\uff08hoisting\uff09\u884c\u4e3a\u4ee5\u53ca<code>this<\/code>\u7ed1\u5b9a\u7b49\u7279\u6027\u3002<\/p>\n<\/p>\n<p><p><strong>\u51fd\u6570\u7c7b\u578b<\/strong>\u4e3b\u8981\u5305\u62ec\u547d\u540d\u51fd\u6570\u3001\u533f\u540d\u51fd\u6570\u3001\u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f\uff08IIFE\uff09\u3001\u56de\u8c03\u51fd\u6570\uff0c\u4ee5\u53ca\u9ad8\u9636\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c<\/h3>\n<\/p>\n<p><p>\u5728JavaScript\u4e2d\uff0c\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u5355\u4e2a\u503c\u3002\u53c2\u6570\u53ef\u4ee5\u662f\u5305\u62ec\u5176\u4ed6\u51fd\u6570\u5728\u5185\u7684\u4efb\u4f55JavaScript\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u9ed8\u8ba4\u53c2\u6570<\/strong>\uff1a\u5728\u6ca1\u6709\u4f20\u9012\u503c\u6216\u4f20\u9012<code>undefined<\/code>\u65f6\u4f7f\u7528\u7684\u5907\u7528\u503c\u3002<\/li>\n<li><strong>\u5269\u4f59\u53c2\u6570<\/strong>\uff08rest parameter syntax\uff09\uff1a\u5141\u8bb8\u6211\u4eec\u5c06\u4e00\u4e2a\u4e0d\u786e\u5b9a\u6570\u91cf\u7684\u53c2\u6570\u8868\u793a\u4e3a\u4e00\u4e2a\u6570\u7ec4\u3002<\/li>\n<li><strong>\u6790\u6784\u8d4b\u503c<\/strong>\uff08destructuring\uff09\uff1a\u5728\u51fd\u6570\u7b7e\u540d\u4e2d\u76f4\u63a5\u63d0\u53d6\u5bf9\u8c61\u5c5e\u6027\u6216\u6570\u7ec4\u4e2d\u7684\u503c\u3002<\/li>\n<\/ul>\n<p><p>\u8fd4\u56de\u503c\u901a\u8fc7<code>return<\/code>\u8bed\u53e5\u8fd4\u56de\uff0c\u5982\u679c\u7701\u7565<code>return<\/code>\u8bed\u53e5\u6216\u8fd4\u56de\u65e0\u503c\uff0c\u5219\u51fd\u6570\u9ed8\u8ba4\u8fd4\u56de<code>undefined<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u51fd\u6570\u4f5c\u7528\u57df\u548c\u95ed\u5305<\/h3>\n<\/p>\n<p><p><strong>\u51fd\u6570\u4f5c\u7528\u57df<\/strong>\u662f\u6307\u53d8\u91cf\u5728\u51fd\u6570\u5185\u90e8\u58f0\u660e\u65f6\uff0c\u53ea\u80fd\u5728\u8be5\u51fd\u6570\u5185\u90e8\u88ab\u8bbf\u95ee\uff0c\u8fd9\u662f\u56e0\u4e3aJavaScript\u4f7f\u7528\u8bcd\u6cd5\u4f5c\u7528\u57df\u6216\u9759\u6001\u4f5c\u7528\u57df\uff0c\u610f\u5473\u7740\u51fd\u6570\u7684\u6267\u884c\u4f9d\u8d56\u4e8e\u53d8\u91cf\u5728\u4ee3\u7801\u7f16\u5199\u65f6\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p><strong>\u95ed\u5305<\/strong>\u662f\u6307\u6709\u6743\u8bbf\u95ee\u53e6\u4e00\u4e2a\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u53d8\u91cf\u7684\u51fd\u6570\uff0c\u5b83\u662fJavaScript\u5f3a\u5927\u7279\u6027\u548c\u5e38\u89c1\u6a21\u5f0f\u3002\u95ed\u5305\u7684\u4f7f\u7528\u53ef\u4ee5\u5728\u5185\u90e8\u51fd\u6570\u4e2d\u4fdd\u6301\u5bf9\u5916\u90e8\u51fd\u6570\u4f5c\u7528\u57df\u7684\u5f15\u7528\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u5df2\u7ecf\u6267\u884c\u7ed3\u675f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u9ad8\u7ea7\u51fd\u6570\u7279\u6027<\/h3>\n<\/p>\n<p><p>JavaScript\u4e2d\u7684\u9ad8\u7ea7\u51fd\u6570\u7279\u6027\uff0c\u5982<strong>\u56de\u8c03\u51fd\u6570\u3001Promise\u3001\u5f02\u6b65\/\u7b49\u5f85\uff08Async\/Aw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t\uff09<\/strong>\uff0c\u6269\u5c55\u4e86\u51fd\u6570\u7684\u80fd\u529b\uff0c\u5141\u8bb8\u6211\u4eec\u5904\u7406\u5f02\u6b65\u4ee3\u7801\uff0c\u4ee5\u53ca\u6784\u5efa\u66f4\u590d\u6742\u7684\u6d41\u7a0b\u63a7\u5236\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u56de\u8c03\u51fd\u6570<\/strong>\uff1a\u662f\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u5728\u90a3\u4e2a\u51fd\u6570\u5b8c\u6210\u540e\u8c03\u7528\u7684\u51fd\u6570\u3002<\/li>\n<li><strong>Promises<\/strong>\uff1a\u8868\u793a\u5f02\u6b65\u64cd\u4f5c\u7684\u6700\u7ec8\u5b8c\u6210\uff08\u6216\u5931\u8d25\uff09\u53ca\u5176\u7ed3\u679c\u503c\u7684\u5bb9\u5668\u3002<\/li>\n<li><strong>Async\/Await<\/strong>\uff1a\u662f\u4e00\u79cd\u7f16\u5199\u5f02\u6b65\u4ee3\u7801\u7684\u65b0\u65b9\u6cd5\uff0c\u5b83\u5efa\u7acb\u5728Promises\u4e4b\u4e0a\uff0c\u4f46\u4e0e\u4f20\u7edf\u7684\u56de\u8c03\u548c\u94fe\u5f0f\u8c03\u7528\u4e0d\u540c\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u5bf9\u8fd9\u4e9b\u529f\u80fd\u7684\u5e94\u7528\u548c\u7406\u89e3\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u7ba1\u7406\u548c\u7ec4\u7ec7JavaScript\u4ee3\u7801\uff0c\u5904\u7406\u590d\u6742\u7684\u7f16\u7a0b\u573a\u666f\uff0c\u5e76\u5199\u51fa\u65e2\u5f3a\u5927\u53c8\u5bb9\u6613\u7ef4\u62a4\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. JavaScript\u51fd\u6570\u7684\u5b9a\u4e49\u548c\u8c03\u7528\u65b9\u6cd5\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\nJavaScript\u4e2d\u53ef\u4ee5\u901a\u8fc7function\u5173\u952e\u5b57\u5b9a\u4e49\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u8868\u8fbe\u5f0f\u7684\u65b9\u5f0f\u5b9a\u4e49\u533f\u540d\u51fd\u6570\u3002\u51fd\u6570\u8c03\u7528\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u540d\u540e\u52a0\u62ec\u53f7\u7684\u65b9\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7call()\u6216apply()\u65b9\u6cd5\u6539\u53d8\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u5728JavaScript\u51fd\u6570\u4e2d\u4f20\u9012\u53c2\u6570\uff1f<\/strong><br \/>\nJavaScript\u51fd\u6570\u53ef\u4ee5\u63a5\u6536\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u51fd\u6570\u540d\u540e\u7684\u62ec\u53f7\u4e2d\u58f0\u660e\u53c2\u6570\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7arguments\u5bf9\u8c61\u6765\u8bbf\u95ee\u51fd\u6570\u4f20\u9012\u7684\u53c2\u6570\u3002\u53c2\u6570\u53ef\u4ee5\u662f\u4efb\u610f\u7c7b\u578b\uff0c\u5305\u62ec\u57fa\u672c\u7c7b\u578b\u548c\u5bf9\u8c61\u3002<\/p>\n<p><strong>3. JavaScript\u51fd\u6570\u7684\u8fd4\u56de\u503c\u6709\u54ea\u4e9b\u5904\u7406\u65b9\u5f0f\uff1f<\/strong><br \/>\nJavaScript\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528return\u8bed\u53e5\u6765\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u4e5f\u53ef\u4ee5\u7701\u7565return\u8bed\u53e5\u6765\u8fd4\u56deundefined\u3002\u51fd\u6570\u7684\u8fd4\u56de\u503c\u53ef\u4ee5\u88ab\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u88ab\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u6216\u4f5c\u4e3a\u5176\u4ed6\u51fd\u6570\u7684\u53c2\u6570\u4f7f\u7528\u3002\u5982\u679c\u51fd\u6570\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u7684\u8868\u8fbe\u5f0f\u5c06\u4f1a\u8fd4\u56deundefined\u3002<\/p>\n<p>\u53c2\u8003\u8d44\u6e90\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/JavaScript\/Guide\/Functions\">JavaScript \u51fd\u6570 &#8211; MDN web docs<\/a><\/li>\n<li><a href=\"https:\/\/flaviocopes.com\/javascript-functions\/\">Learning Advanced JavaScript: Functions<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"\u51fd\u6570\u662fJavaScript\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u57fa\u7840\u6784\u4ef6\u4e4b\u4e00\uff0c\u5b83\u4eec\u7528\u4e8e\u5c01\u88c5\u4e00\u6bb5\u53ef\u4ee5\u5728\u591a\u5904\u6267\u884c\u7684\u4ee3\u7801\u3002\u51fd\u6570\u7684\u5de5\u4f5c\u539f\u7406\u3001\u51fd\u6570 [&hellip;]","protected":false},"author":3,"featured_media":315171,"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\/315162"}],"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=315162"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/315162\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/315171"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=315162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=315162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=315162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}