{"id":159408,"date":"2024-05-08T13:09:11","date_gmt":"2024-05-08T05:09:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/159408.html"},"modified":"2024-05-08T13:09:15","modified_gmt":"2024-05-08T05:09:15","slug":"%e5%a6%82%e6%9e%9cjavascript%e6%94%af%e6%8c%81lambda%e8%a1%a8%e8%be%be%e5%bc%8f%e4%bc%9a%e6%9c%89%e4%bb%80%e4%b9%88%e7%9a%84%e6%95%88%e6%9e%9c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/159408.html","title":{"rendered":"\u5982\u679cJavaScript\u652f\u6301lambda\u8868\u8fbe\u5f0f\u4f1a\u6709\u4ec0\u4e48\u7684\u6548\u679c"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26221847\/098e71be-fc5b-4d6e-b762-a64aee1327f9.webp\" alt=\"\u5982\u679cJavaScript\u652f\u6301lambda\u8868\u8fbe\u5f0f\u4f1a\u6709\u4ec0\u4e48\u7684\u6548\u679c\" \/><\/p>\n<p><p>JavaScript \u7684 lambda \u8868\u8fbe\u5f0f\uff0c\u5373\u7bad\u5934\u51fd\u6570\uff08Arrow functions\uff09\uff0c<strong>\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u4e3a\u7b80\u6d01\u7684\u51fd\u6570\u5199\u6cd5\u3001\u6539\u8fdb\u4e86this\u6307\u9488\u7684\u5904\u7406\u3001\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb<\/strong>\u3002\u5b83\u4eec\u5728\u5904\u7406\u56de\u8c03\u51fd\u6570\u548c\u95ed\u5305\u65f6\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u80fd\u6355\u83b7\u5f53\u524d\u4e0a\u4e0b\u6587\u4e2d\u7684 <code>this<\/code> \u503c\u3002<\/p>\n<\/p>\n<p><p>\u7bad\u5934\u51fd\u6570\u4e0d\u5177\u5907\u81ea\u5df1\u7684 <code>this<\/code> \u7ed1\u5b9a\uff0c\u5bfc\u81f4 <code>this<\/code> \u5b9e\u9645\u4e0a\u662f\u5728\u5b9a\u4e49\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u4e2d\u5bfb\u627e\u3002\u8fd9\u89e3\u51b3\u4e86\u4f20\u7edf\u51fd\u6570\u56e0\u521b\u5efa\u65b0\u4f5c\u7528\u57df\u800c\u5f15\u8d77\u7684 <code>this<\/code> \u6307\u9488\u6df7\u4e71\u7684\u95ee\u9898\u3002\u5728\u4e8b\u4ef6\u5904\u7406\u5668\u6216\u8005\u4f7f\u7528\u56de\u8c03\u6a21\u5f0f\u7684\u573a\u666f\u4e2d\uff0c\u8fd9\u4e00\u7279\u6027\u6781\u5927\u5730\u7b80\u5316\u4e86\u4ee3\u7801\uff0c\u4e14\u8ba9<code>this<\/code>\u7684\u884c\u4e3a\u53d8\u5f97\u53ef\u9884\u6d4b\u3002\u6b64\u5916\uff0c\u7bad\u5934\u51fd\u6570\u6709\u52a9\u4e8e\u4f7f\u5f97\u4ee3\u7801\u91cf\u51cf\u5c11\uff0c\u5c24\u5176\u662f\u5728\u53ea\u6709\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e14\u76f4\u63a5\u8fd4\u56de\u7ed3\u679c\u503c\u7684\u573a\u666f\u4e0b\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e00\u3001\u4ee3\u7801\u7b80\u6d01\u6027<\/strong><\/h3>\n<\/p>\n<p><p>\u7bad\u5934\u51fd\u6570\u6700\u663e\u8457\u7684\u597d\u5904\u662f\u8ba9\u4ee3\u7801\u53d8\u5f97\u66f4\u52a0\u7b80\u6d01\u3002\u5728\u5355\u8868\u8fbe\u5f0f\u7684\u573a\u666f\u4e0b\uff0c\u53ef\u4ee5\u7701\u7565\u82b1\u62ec\u53f7 <code>{}<\/code> \u548c <code>return<\/code> \u5173\u952e\u5b57\u3002\u4f8b\u5982\uff0c\u5bf9\u4e00\u4e2a\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u53ef\u4ee5\u7531\u4f20\u7edf\u7684\u533f\u540d\u51fd\u6570\u7b80\u5316\u4e3a\u7bad\u5934\u51fd\u6570\uff0c\u4ee3\u7801\u91cf\u663e\u8457\u51cf\u5c11\uff0c\u4e14\u9605\u8bfb\u6027\u63d0\u9ad8\u3002<\/p>\n<\/p>\n<ul>\n<li>\u4f20\u7edf\u51fd\u6570\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-javascript\">array.sort(function(a, b) {<\/p>\n<p>  return a - b;<\/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\">array.sort((a, b) =&gt; a - b);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><strong>\u4e8c\u3001\u65e0\u7ed1\u5b9a\u7684this<\/strong><\/h3>\n<\/p>\n<p><p>\u7bad\u5934\u51fd\u6570\u201c\u7ee7\u627f\u201d\u4e86\u5b83\u6240\u5728\u4e0a\u4e0b\u6587\u7684 <code>this<\/code> \u503c\uff0c\u8fd9\u6837\u5c31\u51cf\u5c11\u4e86\u56e0\u4e3a <code>this<\/code> \u6307\u9488\u4e0d\u660e\u6240\u5bfc\u81f4\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<ul>\n<li>\u4f20\u7edf\u51fd\u6570\u4e2d\u7684\u95ee\u9898\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-javascript\">function Timer() {<\/p>\n<p>  this.seconds = 0;<\/p>\n<p>  setInterval(function() {<\/p>\n<p>    this.seconds++; \/\/ \u8fd9\u91cc\u7684this\u5e76\u4e0d\u6307\u5411Timer\u5b9e\u4f8b<\/p>\n<p>  }, 1000);<\/p>\n<p>}<\/p>\n<p>var timer = new Timer();<\/p>\n<p>console.log(timer.seconds); \/\/ \u603b\u662f0\uff0c\u56e0\u4e3asetInterval\u4e2d\u7684\u533f\u540d\u51fd\u6570\u6709\u81ea\u5df1\u7684this<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u540e\u7684\u89e3\u51b3\u65b9\u6848\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-javascript\">function Timer() {<\/p>\n<p>  this.seconds = 0;<\/p>\n<p>  setInterval(() =&gt; {<\/p>\n<p>    this.seconds++; \/\/ \u7bad\u5934\u51fd\u6570\u6ca1\u6709\u81ea\u5df1\u7684this\uff0c\u6240\u4ee5\u8fd9\u91cc\u7684this\u6307\u5411Timer\u5b9e\u4f8b<\/p>\n<p>  }, 1000);<\/p>\n<p>}<\/p>\n<p>var timer = new Timer();<\/p>\n<p>console.log(timer.seconds); \/\/ \u6bcf\u8fc71\u79d2\u589e\u52a01<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><strong>\u4e09\u3001\u6ca1\u6709arguments\u5bf9\u8c61<\/strong><\/h3>\n<\/p>\n<p><p>\u7bad\u5934\u51fd\u6570\u4e5f\u6ca1\u6709\u81ea\u5df1\u7684 <code>arguments<\/code> \u5bf9\u8c61\u3002\u8fd9\u610f\u5473\u7740\u5728\u5199\u9700\u8981\u8bbf\u95ee <code>arguments<\/code> \u5bf9\u8c61\u7684\u51fd\u6570\u65f6\u8981\u66f4\u52a0\u5c0f\u5fc3\uff0c\u53ef\u80fd\u9700\u8981\u91c7\u7528\u5269\u4f59\u53c2\u6570\uff08<code>...args<\/code>\uff09\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ul>\n<li>\u4f20\u7edf\u51fd\u6570\u4e2d\u7684 <code>arguments<\/code>:<\/li>\n<\/ul>\n<p><pre><code class=\"language-javascript\">function concatenateAll() {<\/p>\n<p>  return Array.prototype.join.call(arguments);<\/p>\n<p>}<\/p>\n<p>console.log(concatenateAll(&#039;Hello&#039;, &#039;World&#039;, &#039;!&#039;)); \/\/ &quot;HelloWorld!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u5728\u7bad\u5934\u51fd\u6570\u4e2d\u4f7f\u7528\u5269\u4f59\u53c2\u6570\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-javascript\">const concatenateAll = (...args) =&gt; args.join(&#039;&#039;);<\/p>\n<p>console.log(concatenateAll(&#039;Hello&#039;, &#039;World&#039;, &#039;!&#039;)); \/\/ &quot;HelloWorld!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><strong>\u56db\u3001\u4e0d\u9002\u7528\u573a\u666f<\/strong><\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1\u7bad\u5934\u51fd\u6570\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\u90fd\u975e\u5e38\u6709\u7528\uff0c\u4f46\u662f\u5b83\u4eec\u5e76\u4e0d\u9002\u5408\u6240\u6709\u573a\u666f\u3002\u4f8b\u5982\uff0c\u4e0d\u80fd\u5c06\u7bad\u5934\u51fd\u6570\u7528\u4f5c\u6784\u9020\u51fd\u6570\uff0c\u5c1d\u8bd5\u7528 <code>new<\/code> \u5173\u952e\u5b57\u8c03\u7528\u7bad\u5934\u51fd\u6570\u4f1a\u629b\u51fa\u9519\u8bef\u3002\u540c\u6837\uff0c\u7bad\u5934\u51fd\u6570\u4e5f\u6ca1\u6709 <code>prototype<\/code> \u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u5728\u5bf9\u8c61\u5b57\u9762\u91cf\u4e2d\u5b9a\u4e49\u65b9\u6cd5\u65f6\uff0c\u7bad\u5934\u51fd\u6570\u53ef\u80fd\u5e76\u4e0d\u662f\u6700\u4f73\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u5c06\u65e0\u6cd5\u6b63\u786e\u7ed1\u5b9a\u5230\u5bf9\u8c61\u7684 <code>this<\/code> \u4e0a\u3002<\/p>\n<\/p>\n<ul>\n<li>\u4f20\u7edf\u65b9\u6cd5\u5b9a\u4e49\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-javascript\">const obj = {<\/p>\n<p>  value: 1,<\/p>\n<p>  increment: function() {<\/p>\n<p>    this.value++;<\/p>\n<p>  }<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u4e0d\u63a8\u8350\u7684\u7bad\u5934\u51fd\u6570\u65b9\u6cd5\u5b9a\u4e49\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-javascript\">const obj = {<\/p>\n<p>  value: 1,<\/p>\n<p>  increment: () =&gt; this.value++<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><strong>\u4e94\u3001\u5728\u56de\u8c03\u4e2d\u7684\u4f18\u52bf<\/strong><\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u548c\u56de\u8c03\u65f6\uff0c\u7bad\u5934\u51fd\u6570\u5e26\u6765\u7684\u4f18\u52bf\u5341\u5206\u660e\u663e\u3002\u901a\u8fc7\u5176\u8bcd\u6cd5\u4f5c\u7528\u57df\u7ed1\u5b9a <code>this<\/code> \u7279\u6027\uff0c\u53ef\u4ee5\u975e\u5e38\u4f18\u96c5\u5730\u5904\u7406 <code>setTimeout<\/code>\u3001<code>setInterval<\/code>\u3001\u6216\u662f\u5f02\u6b65\u8bf7\u6c42\u7684\u56de\u8c03\u51fd\u6570\u3002<\/p>\n<\/p>\n<ul>\n<li>\u4f20\u7edf\u7684\u56de\u8c03\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-javascript\">const self = this;<\/p>\n<p>doAsyncOperation(function(result) {<\/p>\n<p>  self.value = result;<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u4f7f\u7528\u7bad\u5934\u51fd\u6570\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-javascript\">doAsyncOperation((result) =&gt; {<\/p>\n<p>  this.value = result; \/\/ this\u6307\u5411\u5b9a\u4e49\u65f6\u7684\u4f5c\u7528\u57df\uff0c\u4ee3\u7801\u66f4\u6613\u4e8e\u7406\u89e3<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><strong>\u516d\u3001\u603b\u7ed3<\/strong><\/h3>\n<\/p>\n<p><p>JavaScript\u4e2d\u7684\u7bad\u5934\u51fd\u6570\u4e0d\u53ea\u662f\u4e00\u4e2a\u8bed\u6cd5\u7cd6\uff0c\u5b83\u63d0\u4f9b\u4e86\u5bf9\u4e8e <code>this<\/code> \u7684\u7ed1\u5b9a\u548c\u51fd\u6570\u7b80\u5199\u7684\u53cc\u91cd\u4f18\u52bf\u3002\u5728\u65e5\u5e38\u7f16\u7a0b\u5b9e\u8df5\u4e2d\uff0c\u5b83\u4eec\u4e3b\u8981\u7528\u4e8e\u90a3\u4e9b\u9700\u8981\u4ee5\u7b80\u5355\u7684\u65b9\u5f0f\u5904\u7406 <code>this<\/code> \u7ed1\u5b9a\u6216\u8005\u5199\u66f4\u5c11\u4ee3\u7801\u7684\u51fd\u6570\u8868\u8fbe\u5f0f\u573a\u666f\u3002\u7136\u800c\uff0c\u7531\u4e8e\u5b83\u4eec\u7684\u7279\u6b8a\u6027\u8d28\uff0c\u4e0d\u80fd\u7528\u4f5c\u6784\u9020\u51fd\u6570\uff0c\u4e5f\u4e0d\u9002\u7528\u4e8e\u6240\u6709\u7684\u4e0a\u4e0b\u6587\uff08\u5982\u5b9a\u4e49\u5bf9\u8c61\u65b9\u6cd5\u65f6\uff09\u3002\u9002\u5f53\u4f7f\u7528\u65f6\uff0c\u7bad\u5934\u51fd\u6570\u80fd\u5927\u5e45\u63d0\u5347\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. JavaScript\u652f\u6301lambda\u8868\u8fbe\u5f0f\u7684\u6548\u679c\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>Lambda\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u533f\u540d\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u66f4\u7b80\u6d01\u5730\u7f16\u5199\u548c\u4f7f\u7528\u51fd\u6570\u3002\u5982\u679cJavaScript\u652f\u6301lambda\u8868\u8fbe\u5f0f\uff0c\u5f00\u53d1\u8005\u5c06\u80fd\u591f\u4ee5\u66f4\u7cbe\u7b80\u7684\u65b9\u5f0f\u7f16\u5199\u51fd\u6570\uff0c\u51cf\u5c11\u4ee3\u7801\u91cf\u5e76\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<p><strong>2. \u4e3a\u4ec0\u4e48JavaScript\u7684lambda\u8868\u8fbe\u5f0f\u5bf9\u5f00\u53d1\u4eba\u5458\u5f88\u6709\u5e2e\u52a9\uff1f<\/strong><br \/>Lambda\u8868\u8fbe\u5f0f\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\u3001\u6613\u8bfb\u548c\u6613\u7ef4\u62a4\u3002\u901a\u8fc7\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u5728\u4e0d\u5b9a\u4e49\u5177\u4f53\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\u76f4\u63a5\u4f20\u9012\u533f\u540d\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u6216\u56de\u8c03\u51fd\u6570\uff0c\u5e76\u4e14\u80fd\u591f\u66f4\u65b9\u4fbf\u5730\u5904\u7406\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u4efb\u52a1\u548c\u64cd\u4f5c\u3002<\/p>\n<p><strong>3. JavaScript\u652f\u6301lambda\u8868\u8fbe\u5f0f\u5c06\u5982\u4f55\u4fc3\u8fdb\u7f16\u7a0b\u8303\u5f0f\u7684\u53d1\u5c55\uff1f<\/strong><br \/>Lambda\u8868\u8fbe\u5f0f\u662f\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u6838\u5fc3\u6982\u5ff5\uff0c\u800cJavaScript\u4f5c\u4e3a\u4e00\u95e8\u73b0\u4ee3\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5982\u679c\u80fd\u591f\u652f\u6301lambda\u8868\u8fbe\u5f0f\uff0c\u5c06\u80fd\u591f\u66f4\u5168\u9762\u5730\u652f\u6301\u51fd\u6570\u5f0f\u7f16\u7a0b\u8303\u5f0f\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u4f7f\u7528\u51fd\u6570\u4f5c\u4e3a\u4e00\u7b49\u516c\u6c11\u6765\u8fdb\u884c\u7f16\u7a0b\uff0c\u4ece\u800c\u4fc3\u8fdb\u7f16\u7a0b\u8303\u5f0f\u7684\u53d1\u5c55\u548c\u5e94\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"JavaScript \u7684 lambda \u8868\u8fbe\u5f0f\uff0c\u5373\u7bad\u5934\u51fd\u6570\uff08Arrow functions\uff09\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u4e3a\u7b80 [&hellip;]","protected":false},"author":3,"featured_media":159417,"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\/159408"}],"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=159408"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/159408\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/159417"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=159408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=159408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=159408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}