{"id":165997,"date":"2024-05-08T15:27:46","date_gmt":"2024-05-08T07:27:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/165997.html"},"modified":"2024-05-08T15:27:52","modified_gmt":"2024-05-08T07:27:52","slug":"javascript%e4%b8%ad%e7%9a%84%e6%99%ae%e9%80%9a%e5%87%bd%e6%95%b0%e5%92%8c%e7%ae%ad%e5%a4%b4%e5%87%bd%e6%95%b0%e6%9c%89%e4%bd%95%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/165997.html","title":{"rendered":"javascript\u4e2d\u7684\u666e\u901a\u51fd\u6570\u548c\u7bad\u5934\u51fd\u6570\u6709\u4f55\u533a\u522b"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26233008\/3fd8a7c2-eca3-461f-90fb-1a0057180d4c.webp\" alt=\"javascript\u4e2d\u7684\u666e\u901a\u51fd\u6570\u548c\u7bad\u5934\u51fd\u6570\u6709\u4f55\u533a\u522b\" \/><\/p>\n<p><p>\u666e\u901a\u51fd\u6570\u548c\u7bad\u5934\u51fd\u6570\u5728JavaScript\u4e2d\u90fd\u662f\u7528\u6765\u5b9a\u4e49\u51fd\u6570\u7684\u65b9\u6cd5\uff0c\u4f46\u5b83\u4eec\u5728<strong>\u8bed\u6cd5\u7b80\u6d01\u6027\u3001this\u7ed1\u5b9a\u673a\u5236\u3001\u6784\u9020\u51fd\u6570\u80fd\u529b\u3001\u4f7f\u7528arguments\u5bf9\u8c61\u3001\u9002\u7528\u573a\u666f<\/strong>\u7b49\u65b9\u9762\u5b58\u5728\u4e0d\u540c\u3002<strong>\u666e\u901a\u51fd\u6570<\/strong>\u662f\u4f20\u7edf\u7684JavaScript\u51fd\u6570\u5b9a\u4e49\u65b9\u5f0f\uff0c\u4e5f\u79f0\u4e3a\u51fd\u6570\u58f0\u660e\u6216\u51fd\u6570\u8868\u8fbe\u5f0f\u3002\u5b83\u4f7f\u7528\u5173\u952e\u5b57function\uff0c\u53ef\u4ee5\u5177\u6709\u4e00\u4e2a\u540d\u79f0\uff08\u4e5f\u53ef\u4ee5\u662f\u533f\u540d\u7684\uff09\uff0c\u5e76\u5141\u8bb8\u5728\u51fd\u6570\u4f53\u5185\u90e8\u8bbf\u95eethis\u5173\u952e\u5b57\u3001\u4f7f\u7528arguments\u5bf9\u8c61\u3001\u88abnew\u64cd\u4f5c\u7b26\u8c03\u7528\u4ee5\u6784\u9020\u5bf9\u8c61\u3002<strong>\u7bad\u5934\u51fd\u6570<\/strong>\u5219\u662f\u5728ES6\u4e2d\u65b0\u589e\u7684\uff0c\u4f7f\u7528\u7bad\u5934\uff08=&gt;\uff09\u7684\u7b80\u6d01\u8bed\u6cd5\u6765\u5199\u3002\u7bad\u5934\u51fd\u6570\u4e0d\u521b\u5efa\u81ea\u5df1\u7684this\u503c\uff0c\u800c\u662f\u6355\u83b7\u5176\u6240\u5728\u4e0a\u4e0b\u6587\u7684this\u503c\uff0c\u56e0\u6b64\u5b83\u5728\u56de\u8c03\u51fd\u6570\u548c\u65b9\u6cd5\u94fe\u4e2d\u5c24\u5176\u6709\u7528\u3002\u5b83\u4e0d\u80fd\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\uff0c\u4e5f\u4e0d\u63d0\u4f9barguments\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\u6211\u4eec\u4f1a\u8be6\u7ec6\u89e3\u6790\u8fd9\u4e9b\u533a\u522b\uff0c\u5e76\u8ba8\u8bba\u5b83\u4eec\u5728\u65e5\u5e38\u7f16\u7a0b\u4e2d\u7684\u5177\u4f53\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8bed\u6cd5\u7b80\u6d01\u6027<\/h3>\n<\/p>\n<p><p>\u7bad\u5934\u51fd\u6570\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u7684\u8bed\u6cd5\u6765\u5b9a\u4e49\u51fd\u6570\u3002\u5b83\u4e0d\u4f7f\u7528function\u5173\u952e\u5b57\uff0c\u800c\u662f\u5c06\u53c2\u6570\u548c\u51fd\u6570\u4f53\u901a\u8fc7\u7bad\u5934\uff08=&gt;\uff09\u8fde\u63a5\u8d77\u6765\u3002\u5982\u679c\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\u4e14\u4e0d\u9700\u8981\u7f16\u5199\u51fd\u6570\u4f53\uff08\u5373\u76f4\u63a5\u8fd4\u56de\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u65f6\uff09\uff0c\u751a\u81f3\u53ef\u4ee5\u7701\u7565\u53c2\u6570\u5468\u56f4\u7684\u62ec\u53f7\u4ee5\u53ca\u51fd\u6570\u4f53\u5468\u56f4\u7684\u82b1\u62ec\u53f7\u548creturn\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><p>\u666e\u901a\u51fd\u6570\u4f8b\u5b50:<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function add(a, b) {<\/p>\n<p>  return a + b;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7bad\u5934\u51fd\u6570\u4f8b\u5b50:<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const add = (a, b) =&gt; a + b;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001this\u7ed1\u5b9a\u673a\u5236<\/h3>\n<\/p>\n<p><p>\u7bad\u5934\u51fd\u6570\u4e0d\u7ed1\u5b9a\u81ea\u5df1\u7684this\uff0c\u5b83\u4f1a\u6355\u83b7\u5176\u6240\u5728\uff08\u5b9a\u4e49\u65f6\u7684\uff09\u4e0a\u4e0b\u6587\u7684this\u503c\u4f5c\u4e3a\u81ea\u5df1\u7684this\u503c\u3002\u8fd9\u4e0e\u666e\u901a\u51fd\u6570\u4e0d\u540c\uff0c\u666e\u901a\u51fd\u6570\u7684this\u503c\u901a\u5e38\u53d6\u51b3\u4e8e\u51fd\u6570\u7684\u8c03\u7528\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u5728\u5bf9\u8c61\u65b9\u6cd5\u4e2d<\/strong>\uff0c\u5982\u679c\u4e00\u4e2a\u65b9\u6cd5\u662f\u7528\u666e\u901a\u51fd\u6570\u5b9a\u4e49\u7684\uff0c\u90a3\u4e48\u5f53\u8fd9\u4e2a\u65b9\u6cd5\u88ab\u8c03\u7528\u65f6\uff0cthis\u901a\u5e38\u6307\u5411\u8c03\u7528\u8be5\u65b9\u6cd5\u7684\u5bf9\u8c61\u3002\u7136\u800c\uff0c\u5982\u679c\u5728\u65b9\u6cd5\u5185\u90e8\u6709\u4e00\u4e2a\u5185\u5d4c\u7684\u51fd\u6570\uff08\u4f8b\u5982\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff09\uff0c\u8fd9\u4e2a\u5185\u5d4c\u7684\u666e\u901a\u51fd\u6570\u4e0d\u4f1a\u81ea\u52a8\u7ee7\u627f\u65b9\u6cd5\u7684this\u7ed1\u5b9a\uff0c\u9664\u975e\u4f7f\u7528.bind(this)\u3001\u4e00\u4e2a\u95ed\u5305\uff0c\u6216\u8005\u4e00\u4e2a\u53d8\u91cf\u6765\u624b\u52a8\u7ed1\u5b9a\u3002<\/p>\n<\/p>\n<p><p>\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u5982\u679c\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u6765\u5b9a\u4e49\u4e00\u4e2a\u5185\u5d4c\u51fd\u6570\uff0cthis\u4f1a\u81ea\u7136\u7ee7\u627f\uff0c\u56e0\u4e3a\u7bad\u5934\u51fd\u6570\u5728\u8fd9\u65b9\u9762\u6ca1\u6709\u81ea\u5df1\u7684\u7ed1\u5b9a\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6784\u9020\u51fd\u6570\u80fd\u529b<\/h3>\n<\/p>\n<p><p>\u666e\u901a\u51fd\u6570\u53ef\u4ee5\u7528\u4f5c\u6784\u9020\u51fd\u6570\uff0c\u53ef\u4ee5\u4e0enew\u4e00\u8d77\u4f7f\u7528\u521b\u5efa\u65b0\u7684\u5b9e\u4f8b\u3002\u7136\u800c\uff0c\u7bad\u5934\u51fd\u6570\u4e0d\u80fd\u7528\u4f5c\u6784\u9020\u51fd\u6570\u3002\u5982\u679c\u5c1d\u8bd5\u901a\u8fc7new\u8c03\u7528\u7bad\u5934\u51fd\u6570\uff0c\u5c06\u4f1a\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u5bf9\u8c61\uff0c\u5e76\u5e0c\u671b\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684\u539f\u578b\u94fe\u65f6\uff0c\u5e94\u5f53\u4f7f\u7528\u666e\u901a\u51fd\u6570\u3002\u7bad\u5934\u51fd\u6570\u7531\u4e8e\u7f3a\u4e4f\u6784\u9020\u51fd\u6570\u7684\u80fd\u529b\uff0c\u5728\u8fd9\u79cd\u573a\u666f\u4e0b\u4e0d\u9002\u5b9c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528arguments\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u5728\u666e\u901a\u51fd\u6570\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528arguments\u5bf9\u8c61\u6765\u8bbf\u95ee\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u6240\u6709\u53c2\u6570\u3002\u8fd9\u5728\u53c2\u6570\u6570\u91cf\u4e0d\u786e\u5b9a\u6216\u8005\u8981\u64cd\u4f5c\u53c2\u6570\u5217\u8868\u65f6\u975e\u5e38\u6709\u7528\u3002\u7136\u800c\uff0c\u5728\u7bad\u5934\u51fd\u6570\u4e2d\uff0c\u6ca1\u6709arguments\u7ed1\u5b9a\u3002\u60f3\u8981\u5728\u7bad\u5934\u51fd\u6570\u4e2d\u4ee5\u7c7b\u4f3c\u65b9\u5f0f\u8bbf\u95ee\u53c2\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u5269\u4f59\u53c2\u6570\u8bed\u6cd5\uff08&#8230;args\uff09\u6765\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><p>\u6df1\u5165\u63a2\u8ba8arguments\u5bf9\u8c61\u7684\u5e94\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u5904\u7406\u5404\u79cd\u53c2\u6570\u76f8\u5173\u7684\u9700\u6c42\uff0c\u6bd4\u5982\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u63a5\u6536\u4efb\u610f\u6570\u91cf\u7684\u8f93\u5165\u503c\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u603b\u548c\u3002\u4f7f\u7528\u666e\u901a\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u8fd9\u6837\u5199\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function sum() {<\/p>\n<p>  let total = 0;<\/p>\n<p>  for (let i = 0; i &lt; arguments.length; i++) {<\/p>\n<p>    total += arguments[i];<\/p>\n<p>  }<\/p>\n<p>  return total;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bf9\u5e94\u7684\u7bad\u5934\u51fd\u6570\u4f7f\u7528\u5269\u4f59\u53c2\u6570\u8bed\u6cd5\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const sum = (...args) =&gt; args.reduce((total, current) =&gt; total + current, 0);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u7bad\u5934\u51fd\u6570\u7531\u4e8ethis\u7684\u7ed1\u5b9a\u7279\u6027\uff0c\u4f7f\u5f97\u5b83\u975e\u5e38\u9002\u5408\u7528\u4f5c\u56de\u8c03\u51fd\u6570\uff0c\u5c24\u5176\u662f\u5728\u9ad8\u9636\u51fd\u6570\u4e2d\uff0c\u6bd4\u5982map\u3001filter\u548creduce\u3002\u5728\u8fd9\u4e9b\u5df2\u7ecf\u5b9a\u4e49\u4e86\u6267\u884c\u4e0a\u4e0b\u6587\u7684\u573a\u5408\u4e0b\uff0c\u7bad\u5934\u51fd\u6570\u65e0\u9700\u62c5\u5fc3this\u7684\u6307\u5411\u95ee\u9898\u3002\u5b83\u4eec\u4e5f\u975e\u5e38\u9002\u5408\u5728\u4e8b\u4ef6\u5904\u7406\u5668\u548c\u5f02\u6b65\u4ee3\u7801\uff08\u6bd4\u5982Promises\u6216async\/aw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t\uff09\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u4e00\u53e5\uff0c<strong>\u7bad\u5934\u51fd\u6570<\/strong>\u662f\u5bf9\u4f20\u7edf<strong>\u666e\u901a\u51fd\u6570<\/strong>\u7684\u8865\u5145\uff0c\u63d0\u4f9b\u66f4\u7075\u6d3b\u7684\u8bed\u6cd5\u548c\u66f4\u76f4\u89c2\u7684this\u884c\u4e3a\uff0c\u4e3aJavaScript\u5f00\u53d1\u8005\u5e26\u6765\u6781\u5927\u7684\u4fbf\u5229\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u666e\u901a\u51fd\u6570\u548c\u7bad\u5934\u51fd\u6570\u5728\u8bed\u6cd5\u4e0a\u6709\u4f55\u533a\u522b\uff1f<\/strong><\/p>\n<p>\u666e\u901a\u51fd\u6570\u4f7f\u7528 function \u5173\u952e\u5b57\u58f0\u660e\uff0c\u7bad\u5934\u51fd\u6570\u4f7f\u7528 =&gt; \u7b26\u53f7\u58f0\u660e\u3002<\/p>\n<p><strong>2. \u666e\u901a\u51fd\u6570\u548c\u7bad\u5934\u51fd\u6570\u5bf9 this \u5173\u952e\u5b57\u7684\u5904\u7406\u65b9\u5f0f\u6709\u4f55\u4e0d\u540c\uff1f<\/strong><\/p>\n<p>\u666e\u901a\u51fd\u6570\u4e2d\u7684 this \u5173\u952e\u5b57\u6307\u5411\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u7684\u4e0a\u4e0b\u6587\uff0c\u800c\u7bad\u5934\u51fd\u6570\u4e2d\u7684 this \u5173\u952e\u5b57\u6307\u5411\u51fd\u6570\u5b9a\u4e49\u65f6\u7684\u4e0a\u4e0b\u6587\u3002<\/p>\n<p><strong>3. \u666e\u901a\u51fd\u6570\u548c\u7bad\u5934\u51fd\u6570\u9002\u7528\u7684\u573a\u666f\u6709\u54ea\u4e9b\u5dee\u5f02\uff1f<\/strong><\/p>\n<p>\u666e\u901a\u51fd\u6570\u66f4\u9002\u7528\u4e8e\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\u3001\u65b9\u6cd5\u6216\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u4eec\u5177\u6709\u81ea\u5df1\u7684 this \u503c\u3002\u800c\u7bad\u5934\u51fd\u6570\u5219\u9002\u7528\u4e8e\u7b80\u77ed\u7684\u56de\u8c03\u51fd\u6570\u6216\u8005\u9700\u8981\u7ee7\u627f\u7236\u7ea7\u4f5c\u7528\u57df\u7684\u573a\u666f\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e0d\u7ed1\u5b9a\u81ea\u5df1\u7684 this \u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u666e\u901a\u51fd\u6570\u548c\u7bad\u5934\u51fd\u6570\u5728JavaScript\u4e2d\u90fd\u662f\u7528\u6765\u5b9a\u4e49\u51fd\u6570\u7684\u65b9\u6cd5\uff0c\u4f46\u5b83\u4eec\u5728\u8bed\u6cd5\u7b80\u6d01\u6027\u3001this\u7ed1\u5b9a\u673a\u5236\u3001\u6784\u9020\u51fd [&hellip;]","protected":false},"author":3,"featured_media":166010,"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\/165997"}],"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=165997"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/165997\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/166010"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=165997"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=165997"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=165997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}