{"id":166441,"date":"2024-05-08T15:36:40","date_gmt":"2024-05-08T07:36:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/166441.html"},"modified":"2024-05-08T15:36:47","modified_gmt":"2024-05-08T07:36:47","slug":"javascript%e6%9c%89%e8%bf%99%e7%a7%8d%e7%bb%93%e6%9e%84%e5%90%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/166441.html","title":{"rendered":"javascript\u6709\u8fd9\u79cd\u7ed3\u6784\u5417 ( ) =&gt; {"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26233547\/b782fd98-22df-4109-bade-ffc6f2d6819f.webp\"> {&#8221; \/&gt;<\/p>\n<p><p>\u5728JavaScript\u4e2d\uff0c<code>( ) =&gt; { }<\/code> \u786e\u5b9e\u662f\u4e00\u4e2a\u6709\u6548\u7684\u7ed3\u6784\uff0c\u5b83\u8868\u793a\u4e00\u4e2a\u7bad\u5934\u51fd\u6570\uff08Arrow Function\uff09\u3002<strong>\u7bad\u5934\u51fd\u6570<\/strong>\u662f\u5728ES6\/ECMAScript 2015\u4e2d\u5f15\u5165\u7684\uff0c\u4f5c\u7528\u662f\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u7f16\u5199\u51fd\u6570\u8868\u8fbe\u5f0f\u3002\u7bad\u5934\u51fd\u6570\u7684\u8bed\u6cd5\u6bd4\u4f20\u7edf\u7684 JavaScript \u51fd\u6570\u8868\u8fbe\u5f0f\u66f4\u77ed\uff0c\u56e0\u6b64\u662f\u4e00\u4e2a\u975e\u5e38\u53d7\u6b22\u8fce\u7684\u7279\u6027\u3002<\/p>\n<\/p>\n<p><p>\u7bad\u5934\u51fd\u6570\u7279\u522b\u9002\u5408\u7528\u4e8e\u90a3\u4e9b\u9700\u8981\u533f\u540d\u51fd\u6570\u7684\u573a\u5408\uff0c\u4f8b\u5982\u56de\u8c03\u51fd\u6570\u548c\u9ad8\u9636\u51fd\u6570\u3002\u5b83\u4eec\u4e0d\u7ed1\u5b9a\u81ea\u5df1\u7684<code>this<\/code>\u3001<code>arguments<\/code>\u3001<code>super<\/code>\u6216<code>new.target<\/code>\uff0c\u8fd9\u4e9b\u503c\u7531\u5305\u542b\u5b83\u7684\u6700\u8fd1\u4e00\u5c42\u975e\u7bad\u5934\u51fd\u6570\u51b3\u5b9a\u3002\u6b64\u5916\uff0c\u7bad\u5934\u51fd\u6570\u4e0d\u80fd\u7528\u4f5c\u6784\u9020\u5668\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u4e0d\u80fd\u4f7f\u7528<code>new<\/code>\u5173\u952e\u5b57\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u7bad\u5934\u51fd\u6570\u57fa\u672c\u8bed\u6cd5<\/h2>\n<\/p>\n<p><p>\u7bad\u5934\u51fd\u6570\u7684\u57fa\u7840\u8bed\u6cd5\u5f88\u7b80\u5355\u3002<strong>\u5b83\u4ee5\u4e00\u4e2a\u53c2\u6570\u5217\u8868\u5f00\u59cb\uff0c\u7d27\u8ddf\u7740\u4e00\u4e2a\u7bad\u5934\u64cd\u4f5c\u7b26\uff08<code>=&gt;<\/code>\uff09\uff0c\u7136\u540e\u662f\u51fd\u6570\u4f53<\/strong>\u3002\u5982\u679c\u51fd\u6570\u4f53\u53ea\u5305\u542b\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u90a3\u4e48\u82b1\u62ec\u53f7\u548c<code>return<\/code>\u5173\u952e\u5b57\u90fd\u53ef\u4ee5\u7701\u7565\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4e00\u4e2a\u6ca1\u6709\u53c2\u6570\u7684\u7bad\u5934\u51fd\u6570\u53ef\u4ee5\u5199\u6210\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">() =&gt; { <\/p>\n<p>  \/\/ \u51fd\u6570\u4f53<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u7bad\u5934\u51fd\u6570\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\uff0c\u90a3\u4e48\u53ef\u4ee5\u7701\u7565\u53c2\u6570\u7684\u5706\u62ec\u53f7\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">x =&gt; {<\/p>\n<p>  \/\/ \u51fd\u6570\u4f53<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u800c\u5982\u679c\u8981\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u5b57\u9762\u91cf\uff0c\u5219\u5fc5\u987b\u7528\u62ec\u53f7\u5305\u56f4\u5b83\uff0c\u4ee5\u907f\u514d\u4e0e\u51fd\u6570\u4f53\u7684\u82b1\u62ec\u53f7\u6df7\u6dc6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">x =&gt; ({ key: x })<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u7bad\u5934\u51fd\u6570\u4e0e\u4f20\u7edf\u51fd\u6570\u7684\u533a\u522b<\/h2>\n<\/p>\n<p><p>\u7bad\u5934\u51fd\u6570\u4e0e\u4f20\u7edf\u7684\u51fd\u6570\u8868\u8fbe\u5f0f\u6216\u51fd\u6570\u58f0\u660e\u5728\u51e0\u4e2a\u91cd\u8981\u65b9\u9762\u6709\u6240\u4e0d\u540c\uff1a<\/p>\n<\/p>\n<p><p><strong>\u5b83\u4eec\u6ca1\u6709\u81ea\u5df1\u7684<code>this<\/code>\u7ed1\u5b9a<\/strong>\uff1a\u5728\u7bad\u5934\u51fd\u6570\u5185\u90e8\uff0c<code>this<\/code>\u503c\u4e0e\u5305\u56f4\u5b83\u7684\u6700\u8fd1\u4e00\u4e2a\u975e\u7bad\u5934\u51fd\u6570\u7684<code>this<\/code>\u503c\u76f8\u540c\u3002\u8fd9\u662f\u4e0e\u4f20\u7edf\u51fd\u6570\u6700\u5927\u7684\u4e0d\u540c\u4e4b\u5904\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function Person() {<\/p>\n<p>  this.age = 0;<\/p>\n<p>  setInterval(() =&gt; {<\/p>\n<p>    this.age++; \/\/ \u8fd9\u91cc\u7684 this \u6307\u5411\u7684\u662f Person \u5b9e\u4f8b\u5bf9\u8c61<\/p>\n<p>  }, 1000);<\/p>\n<p>}<\/p>\n<p>var p = new Person();<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e0d\u80fd\u7528\u4f5c\u6784\u9020\u51fd\u6570<\/strong>\uff1a\u4f60\u4e0d\u80fd\u4f7f\u7528<code>new<\/code>\u5173\u952e\u5b57\u6765\u8c03\u7528\u7bad\u5934\u51fd\u6570\uff0c\u5982\u679c\u5c1d\u8bd5\u8fd9\u6837\u505a\uff0cJavaScript \u5c06\u629b\u51fa\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p><strong>\u6ca1\u6709<code>arguments<\/code>\u5bf9\u8c61<\/strong>\uff1a\u5728\u7bad\u5934\u51fd\u6570\u5185\u90e8\u662f\u6ca1\u6709<code>arguments<\/code>\u5bf9\u8c61\u7684\u3002\u5982\u679c\u4f60\u9700\u8981\u8bbf\u95ee\u7bad\u5934\u51fd\u6570\u4e2d\u7684\u6240\u6709\u53c2\u6570\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5269\u4f59\u53c2\u6570\u8bed\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const myFunc = (...args) =&gt; {<\/p>\n<p>  \/\/ \u4f7f\u7528 args \u6570\u7ec4\u8bbf\u95ee\u4f20\u9012\u7684\u6240\u6709\u53c2\u6570<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6ca1\u6709<code>prototype<\/code>\u5c5e\u6027<\/strong>\uff1a\u7531\u4e8e\u4f60\u4e0d\u80fd\u4f7f\u7528<code>new<\/code>\u5173\u952e\u5b57\u6765\u8c03\u7528\u7bad\u5934\u51fd\u6570\uff0c\u56e0\u6b64\u5b83\u4eec\u4e5f\u6ca1\u6709<code>prototype<\/code>\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e0d\u80fd\u66f4\u6539<code>this<\/code><\/strong>\uff1a<code>this<\/code>\u7684\u503c\u5728\u7bad\u5934\u51fd\u6570\u7684\u751f\u547d\u5468\u671f\u5185\u662f\u56fa\u5b9a\u7684\uff0c\u4e0e\u5e38\u89c4\u51fd\u6570\u4e0d\u540c\uff0c\u5728\u5e38\u89c4\u51fd\u6570\u4e2d\uff0c<code>this<\/code>\u53ef\u4ee5\u88ab\u663e\u5f0f\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u7bad\u5934\u51fd\u6570\u7684\u4f7f\u7528\u573a\u5408<\/h2>\n<\/p>\n<p><p>\u7bad\u5934\u51fd\u6570\u975e\u5e38\u9002\u5408\u5728\u9700\u8981<strong>\u533f\u540d\u51fd\u6570<\/strong>\u7684\u573a\u5408\uff0c\u5c24\u5176\u662f\u4ee5\u4e0b\u51e0\u4e2a\u5e38\u89c1\u7684\u7528\u4f8b\uff1a<\/p>\n<\/p>\n<p><p><strong>\u4e8b\u4ef6\u5904\u7406\u5668<\/strong>\uff1a\u7bad\u5934\u51fd\u6570\u5e38\u7528\u4f5c\u4e8b\u4ef6\u76d1\u542c\u5668\u4e2d\u7684\u56de\u8c03\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e0d\u4f1a\u521b\u5efa\u81ea\u5df1\u7684<code>this<\/code>\u4e0a\u4e0b\u6587\uff0c\u53ef\u4ee5\u4fdd\u8bc1<code>this<\/code>\u6307\u5411\u6b63\u786e\u7684\u4e0a\u4e0b\u6587\u3002<\/p>\n<\/p>\n<p><p><strong>\u6570\u7ec4\u65b9\u6cd5\u7684\u56de\u8c03<\/strong>\uff1a\u5982<code>map()<\/code>\u3001<code>filter()<\/code>\u3001<code>reduce()<\/code>\u7b49\u6570\u7ec4\u65b9\u6cd5\u4e2d\uff0c\u5e38\u7528\u7bad\u5934\u51fd\u6570\u4f5c\u4e3a\u56de\u8c03\u3002<\/p>\n<\/p>\n<p><p><strong>Promise\u548c\u5f02\u6b65\u51fd\u6570<\/strong>\uff1a\u5728<code>Promise.then()<\/code>\u8c03\u7528\u6216\u8005<code>async\/aw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t<\/code>\u7684\u5f02\u6b65\u51fd\u6570\u4e2d\uff0c\u7bad\u5934\u51fd\u6570\u53ef\u4ee5\u4fdd\u6301<code>this<\/code>\u4e0a\u4e0b\u6587\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u7bad\u5934\u51fd\u6570\u7684\u5c40\u9650\u6027<\/h2>\n<\/p>\n<p><p>\u5c3d\u7ba1\u7bad\u5934\u51fd\u6570\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\uff0c\u4f46\u5b83\u4e5f\u6709\u4e00\u4e9b\u9650\u5236\uff0c\u4e3b\u8981\u5305\u62ec\uff1a<\/p>\n<\/p>\n<p><p><strong>\u4e0d\u9002\u7528\u4e8e\u6240\u6709\u573a\u5408<\/strong>\uff1a\u7bad\u5934\u51fd\u6570\u4e0d\u5e94\u8be5\u7528\u4e8e\u5b9a\u4e49\u5bf9\u8c61\u65b9\u6cd5\u548c\u539f\u578b\u65b9\u6cd5\uff0c\u56e0\u4e3a<code>this<\/code>\u7684\u884c\u4e3a\u4e0e\u671f\u671b\u4e0d\u540c\u3002<\/p>\n<\/p>\n<p><p><strong>\u8c03\u8bd5\u56f0\u96be<\/strong>\uff1a\u7531\u4e8e\u7bad\u5934\u51fd\u6570\u7684<code>this<\/code>\u662f\u4ece\u5916\u56f4\u4ee3\u7801\u4e2d\u6355\u83b7\u7684\uff0c\u8fd9\u53ef\u80fd\u4f1a\u4f7f\u5f97\u8c03\u8bd5\u66f4\u56f0\u96be\uff0c\u5c24\u5176\u662f\u5728\u786e\u5b9a<code>this<\/code>\u7ed1\u5b9a\u65f6\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e0d\u652f\u6301<code>arguments<\/code>\u5bf9\u8c61<\/strong>\uff1a\u5982\u679c\u9700\u8981\u8bbf\u95ee\u6240\u6709\u53c2\u6570\uff0c\u5fc5\u987b\u4f7f\u7528\u5269\u4f59\u53c2\u6570\u8bed\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c<code>( ) =&gt; { }<\/code>\u7ed3\u6784\u5728JavaScript\u4e2d\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u4e14\u9ad8\u6548\u7684\u529f\u80fd\uff0c\u4f46\u662f\u5f00\u53d1\u8005\u5728\u4f7f\u7528\u65f6\u9700\u8981\u6ce8\u610f\u5b83\u4e0e\u4f20\u7edf\u51fd\u6570\u8868\u8fbe\u5f0f\u7684\u4e0d\u540c\u4e4b\u5904\u3002\u901a\u8fc7\u7406\u89e3\u5176\u7279\u70b9\u548c\u5c40\u9650\u6027\uff0c\u53ef\u4ee5\u66f4\u52a0\u5408\u7406\u5730\u5728\u65e5\u5e38\u7f16\u7a0b\u4e2d\u5229\u7528\u7bad\u5934\u51fd\u6570\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u95ee\u98981: JavaScript\u4e2d\u7684 &quot; ( ) =&gt; {} &quot; \u662f\u4ec0\u4e48\u7ed3\u6784\uff1f<\/strong><\/p>\n<p>\u7b54\u68481: \u8fd9\u79cd\u7ed3\u6784\u662f\u7bad\u5934\u51fd\u6570\uff0c\u5b83\u662fES6\u4e2d\u5f15\u5165\u7684\u4e00\u79cd\u65b0\u7684\u51fd\u6570\u5b9a\u4e49\u65b9\u5f0f\u3002\u7bad\u5934\u51fd\u6570\u7684\u8bed\u6cd5\u6bd4\u4f20\u7edf\u7684\u51fd\u6570\u58f0\u660e\u6216\u51fd\u6570\u8868\u8fbe\u5f0f\u66f4\u7b80\u6d01\uff0c\u5b83\u4f7f\u7528\u4e86\u7bad\u5934 (=&gt;) \u64cd\u4f5c\u7b26\u6765\u5b9a\u4e49\u51fd\u6570\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528function\u5173\u952e\u5b57\u3002\u7bad\u5934\u51fd\u6570\u7684\u4e3b\u8981\u7279\u70b9\u662f\u6ca1\u6709\u81ea\u5df1\u7684this\u503c\uff0c\u5b83\u4f1a\u6355\u83b7\u6240\u5728\u4e0a\u4e0b\u6587\u7684this\u503c\u3002<\/p>\n<p><strong>\u95ee\u98982: \u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u7ed3\u6784\uff1a( ) =&gt; {}?<\/strong><\/p>\n<p>\u7b54\u68482: \u7bad\u5934\u51fd\u6570\u7ed3\u6784\u7684\u4e3b\u8981\u4f18\u70b9\u662f\u7b80\u6d01\u6027\u548c\u66f4\u6e05\u6670\u7684\u4ee3\u7801\u7ed3\u6784\u3002\u4e0e\u4f20\u7edf\u7684\u51fd\u6570\u58f0\u660e\u6216\u51fd\u6570\u8868\u8fbe\u5f0f\u76f8\u6bd4\uff0c\u7bad\u5934\u51fd\u6570\u53ef\u4ee5\u66f4\u7b80\u6d01\u5730\u5b9a\u4e49\u51fd\u6570\uff0c\u5c24\u5176\u662f\u5728\u53ea\u6709\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7684\u60c5\u51b5\u4e0b\u3002\u6b64\u5916\uff0c\u7bad\u5934\u51fd\u6570\u4e2d\u7684this\u503c\u4f1a\u81ea\u52a8\u7ed1\u5b9a\u5230\u5176\u5b9a\u4e49\u65f6\u6240\u5728\u7684\u4e0a\u4e0b\u6587\uff0c\u907f\u514d\u4e86\u4f7f\u7528bind()\u6216 that = this \u7684\u9ebb\u70e6\u3002<\/p>\n<p><strong>\u95ee\u98983: \u7bad\u5934\u51fd\u6570\u7ed3\u6784 &quot;( ) =&gt; {}&quot; \u7684\u4f7f\u7528\u573a\u666f\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>\u7b54\u68483: \u7bad\u5934\u51fd\u6570\u7ed3\u6784\u53ef\u4ee5\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u5728\u56de\u8c03\u51fd\u6570\u4e2d\uff0c\u7bad\u5934\u51fd\u6570\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u5b9a\u4e49\u56de\u8c03\u51fd\u6570\u5e76\u4fdd\u6301\u4e0a\u4e0b\u6587\u7684\u4e00\u81f4\u6027\u3002\u6b64\u5916\uff0c\u7bad\u5934\u51fd\u6570\u4e5f\u9002\u7528\u4e8e\u7b80\u5355\u7684\u51fd\u6570\uff0c\u4f8b\u5982\u8ba1\u7b97\u8868\u8fbe\u5f0f\u7684\u503c\u6216\u8fd4\u56de\u7279\u5b9a\u7684\u7ed3\u679c\u3002\u7136\u800c\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7bad\u5934\u51fd\u6570\u5e76\u4e0d\u9002\u7528\u4e8e\u6240\u6709\u573a\u666f\uff0c\u5c24\u5176\u662f\u9700\u8981\u52a8\u6001\u7ed1\u5b9athis\u503c\u6216\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\u4f7f\u7528\u7684\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"{&#8221; \/&gt; \u5728JavaScript\u4e2d\uff0c( ) =&gt; { } \u786e\u5b9e\u662f\u4e00\u4e2a\u6709\u6548\u7684\u7ed3\u6784\uff0c\u5b83\u8868 [&hellip;]","protected":false},"author":3,"featured_media":166454,"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\/166441"}],"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=166441"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/166441\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/166454"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=166441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=166441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=166441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}