{"id":154271,"date":"2024-05-08T11:29:21","date_gmt":"2024-05-08T03:29:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/154271.html"},"modified":"2024-05-08T11:29:25","modified_gmt":"2024-05-08T03:29:25","slug":"%e5%9c%a8-javascript-%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e7%ae%ad%e5%a4%b4%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/154271.html","title":{"rendered":"\u5728 JavaScript \u4e2d\u5982\u4f55\u4f7f\u7528\u7bad\u5934\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26212602\/a4fff24a-00e9-4a63-90f8-005642006456.webp\" alt=\"\u5728 JavaScript \u4e2d\u5982\u4f55\u4f7f\u7528\u7bad\u5934\u51fd\u6570\" \/><\/p>\n<p><p>\u5f53\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u65f6\uff0c<strong>\u5b83\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u5199\u51fd\u6570\u8868\u8fbe\u5f0f<\/strong>\u3001<strong>\u6ca1\u6709\u81ea\u5df1\u7684this\u3001arguments\u3001super\u6216new.target\u7ed1\u5b9a<\/strong>\u3001\u5e76\u4e14\u7ecf\u5e38\u7528\u4e8e\u533f\u540d\u51fd\u6570\u7684\u573a\u666f\u3002\u5b83\u4eec\u4e3b\u8981\u5728\u51fd\u6570\u9700\u8981\u4ee5\u66f4\u77ed\u7684\u5f62\u5f0f\u5199\u51fa\u6216\u8005\u4e0d\u7ed1\u5b9athis\u503c\u65f6\u663e\u5f97\u7279\u522b\u6709\u7528\u3002\u7bad\u5934\u51fd\u6570\u901a\u8fc7\u5728\u53c2\u6570\u548c\u51fd\u6570\u4f53\u4e4b\u95f4\u4f7f\u7528\u201c=&gt;\u201d\u7b26\u53f7\u6765\u5b9a\u4e49\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u7b80\u5355\u7684\u7bad\u5934\u51fd\u6570<code>const add = (a, b) =&gt; a + b;<\/code>\u53ef\u4ee5\u4ee3\u66ff\u4f20\u7edf\u7684\u533f\u540d\u51fd\u6570<code>const add = function(a, b) { return a + b; };<\/code>\u3002\u7bad\u5934\u51fd\u6570\u652f\u6301\u8868\u8fbe\u5f0f\u4f53\u548c\u8bed\u53e5\u5757\u4f53\u4e24\u79cd\u5f62\u5f0f\uff0c\u5bf9\u4e8e\u5355\u4e2a\u8868\u8fbe\u5f0f\u7684\u60c5\u51b5\uff0c\u8fd4\u56de\u503c\u662f\u8be5\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\uff0c\u4e0d\u9700\u8981\u4f7f\u7528<code>return<\/code>\u5173\u952e\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7bad\u5934\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5<\/h3>\n<\/p>\n<p><p>\u7bad\u5934\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u5341\u5206\u7b80\u6d01\u3002\u5982\u679c\u51fd\u6570\u6709\u5355\u4e2a\u53c2\u6570\uff0c\u53ef\u4ee5\u4e0d\u4f7f\u7528\u5706\u62ec\u53f7\uff0c\u76f4\u63a5\u8ddf\u4e0a\u7bad\u5934\u548c\u51fd\u6570\u4f53\u3002\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const square = x =&gt; x * x;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u6ca1\u6709\u53c2\u6570\u6216\u6709\u591a\u4e2a\u53c2\u6570\uff0c\u9700\u8981\u4f7f\u7528\u5706\u62ec\u53f7\u5305\u88f9\u53c2\u6570\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const greet = () =&gt; &#039;Hello World!&#039;;<\/p>\n<p>const sum = (a, b) =&gt; a + b;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5f53\u51fd\u6570\u4f53\u4e0d\u6b62\u4e00\u4e2a\u8868\u8fbe\u5f0f\u65f6\uff0c\u53ef\u4ee5\u7528\u82b1\u62ec\u53f7\u5c06\u4ee3\u7801\u5757\u5305\u56f4\u8d77\u6765\uff0c\u5e76\u663e\u5f0f\u8fd4\u56de\u4e00\u4e2a\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const getTempItem = id =&gt; {<\/p>\n<p>  if (id &gt; 0) {<\/p>\n<p>    return { id: id, status: &#039;active&#039; };<\/p>\n<p>  } else {<\/p>\n<p>    return { id: id, status: &#039;inactive&#039; };<\/p>\n<p>  }<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u7bad\u5934\u51fd\u6570\u548c\u4f20\u7edf\u51fd\u6570\u7684\u533a\u522b<\/h3>\n<\/p>\n<p><p>\u7bad\u5934\u51fd\u6570\u4e0e\u4f20\u7edf\u51fd\u6570\u6700\u663e\u8457\u7684\u533a\u522b\u5728\u4e8e\u5b83\u4eec\u5bf9<code>this<\/code>\u5173\u952e\u5b57\u7684\u5904\u7406\u3002<strong>\u7bad\u5934\u51fd\u6570\u4e0d\u62e5\u6709\u81ea\u5df1\u7684<code>this<\/code>\u503c\uff0c\u800c\u662f\u6355\u83b7\u5176\u521b\u5efa\u4e0a\u4e0b\u6587\u4e2d\u7684<code>this<\/code>\u503c\u4f5c\u4e3a\u81ea\u5df1\u7684<\/strong>\u3002\u8fd9\u610f\u5473\u7740\u5728\u56de\u8c03\u51fd\u6570\u7b49\u573a\u666f\u4e2d\uff0c\u4f60\u4e0d\u9700\u8981\u62c5\u5fc3\u51fd\u6570\u5185\u90e8\u7684<code>this<\/code>\u6307\u5411\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p><strong>\u5728\u4f20\u7edf\u7684\u51fd\u6570\u8868\u8fbe\u5f0f\u4e2d<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function Car() {<\/p>\n<p>  this.speed = 0;<\/p>\n<p>  setInterval(function checkSpeed() {<\/p>\n<p>    \/\/ &#039;this&#039; \u4e0d\u662f &#039;Car&#039; \u5b9e\u4f8b<\/p>\n<p>    console.log(this.speed);<\/p>\n<p>  }, 1000);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f53<code>checkSpeed<\/code>\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c<code>this<\/code>\u6307\u5411\u5168\u5c40\u5bf9\u8c61\uff08\u6216undefined\uff0c\u5982\u679c\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\uff09\uff0c\u800c\u4e0d\u662f<code>Car<\/code>\u7684\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><p><strong>\u800c\u5728\u7bad\u5934\u51fd\u6570\u4e2d<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function Car() {<\/p>\n<p>  this.speed = 0;<\/p>\n<p>  setInterval(() =&gt; {<\/p>\n<p>    \/\/ &#039;this&#039; \u662f &#039;Car&#039; \u5b9e\u4f8b<\/p>\n<p>    console.log(this.speed);<\/p>\n<p>  }, 1000);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>checkSpeed<\/code>\u4f7f\u7528\u4e86\u7bad\u5934\u51fd\u6570\uff0c\u4ece\u800c\u5185\u90e8\u7684<code>this<\/code>\u786e\u5b9e\u5f15\u7528\u4e86<code>Car<\/code>\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u5904\u7406\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>JavaScript\u6570\u7ec4\u5185\u7f6e\u65b9\u6cd5\u5982<code>map<\/code>\u3001<code>filter<\/code>\u3001<code>reduce<\/code>\u7b49\uff0c\u7ecf\u5e38\u4e0e\u7bad\u5934\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\uff0c\u53ef\u4ee5\u5b9e\u73b0\u975e\u5e38\u7b80\u6d01\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f8b\u5982\uff0c\u5728\u4f7f\u7528<code>map<\/code>\u6765\u8f6c\u6362\u4e00\u4e2a\u6570\u7ec4\u65f6<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const numbers = [1, 2, 3, 4];<\/p>\n<p>const squares = numbers.map(x =&gt; x * x);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5f53\u9700\u8981\u591a\u4e2a\u53c2\u6570\u548c\u66f4\u590d\u6742\u7684\u903b\u8f91\u65f6\uff0c\u7bad\u5934\u51fd\u6570\u540c\u6837\u80fd\u6e05\u6670\u8868\u8fbe<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const products = [<\/p>\n<p>  { name: &#039;apple&#039;, type: &#039;fruit&#039; },<\/p>\n<p>  { name: &#039;monitor&#039;, type: &#039;tech&#039; },<\/p>\n<p>  { name: &#039;mango&#039;, type: &#039;fruit&#039; },<\/p>\n<p>  { name: &#039;keyboard&#039;, type: &#039;tech&#039; }<\/p>\n<p>];<\/p>\n<p>const fruitProducts = products.filter(product =&gt; product.type === &#039;fruit&#039;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>filter<\/code>\u548c\u7bad\u5934\u51fd\u6570\u4f18\u96c5\u5730\u7ed3\u5408\u8d77\u6765\uff0c\u7b5b\u9009\u51fa\u4e86\u6240\u6709\u7c7b\u578b\u4e3afruit\u7684\u4ea7\u54c1\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7bad\u5934\u51fd\u6570\u548c\u9ad8\u9636\u51fd\u6570\u7ed3\u5408<\/h3>\n<\/p>\n<p><p>\u9ad8\u9636\u51fd\u6570\u662f\u90a3\u4e9b\u81f3\u5c11\u6ee1\u8db3\u4ee5\u4e0b\u4e00\u4e2a\u6761\u4ef6\u7684\u51fd\u6570\uff1a\u63a5\u53d7\u4e00\u4e2a\u6216\u591a\u4e2a\u51fd\u6570\u4f5c\u4e3a\u8f93\u5165\u3001\u8f93\u51fa\u4e00\u4e2a\u51fd\u6570\u3002\u7531\u4e8e\u7bad\u5934\u51fd\u6570\u7684\u7d27\u51d1\u6027\uff0c\u5b83\u4eec\u5728\u9ad8\u9636\u51fd\u6570\u4e2d\u7684\u5e94\u7528\u975e\u5e38\u6d41\u884c\u3002<\/p>\n<\/p>\n<p><p>\u4e3e\u4e00\u4e2a\u7ec4\u5408\u51fd\u6570\u7684\u4f8b\u5b50\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u53e6\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const withLogging = fn =&gt; (...args) =&gt; {<\/p>\n<p>  console.log(`Calling function ${fn.name}`);<\/p>\n<p>  return fn(...args);<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c<code>withLogging<\/code>\u662f\u4e00\u4e2a\u9ad8\u9636\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u51fd\u6570<code>fn<\/code>\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u3002\u5f53\u65b0\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u5b83\u8bb0\u5f55\u4e00\u4e2a\u6d88\u606f\uff0c\u7136\u540e\u8c03\u7528<code>fn<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7bad\u5934\u51fd\u6570\u7684\u9650\u5236\u548c\u4e0d\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u7bad\u5934\u51fd\u6570\u5bf9\u4e8e\u5f88\u591a\u573a\u666f\u90fd\u975e\u5e38\u4fbf\u5229\uff0c\u4f46\u4e5f\u6709\u5b83\u4eec\u4e0d\u9002\u5408\u7684\u5730\u65b9\u3002\u7bad\u5934\u51fd\u6570\u4e0d\u5e94\u5f53\u7528\u4f5c\u65b9\u6cd5\uff08\u4f1a\u5bfc\u81f4<code>this<\/code>\u7ed1\u5b9a\u95ee\u9898\uff09\u3001\u4e0d\u9002\u5408\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\u4f7f\u7528\uff08\u4e0d\u53ef\u4ee5\u4f7f\u7528<code>new<\/code>\u5173\u952e\u5b57\uff09\u3001\u4e0d\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u4e0a\u4e0b\u6587\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u7279\u522b\u5730\uff0c\u5f53\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u5bf9\u8c61\u65b9\u6cd5\uff0c\u4e14\u8fd9\u4e2a\u65b9\u6cd5\u5185\u90e8\u9700\u8981\u8bbf\u95ee<code>this<\/code>\u65f6\uff0c\u5e94\u5f53\u4f7f\u7528\u4f20\u7edf\u51fd\u6570\u8868\u8fbe\u5f0f\uff0c\u786e\u4fdd<code>this<\/code>\u6b63\u786e\u6307\u5411\u8c03\u7528\u8be5\u65b9\u6cd5\u7684\u5bf9\u8c61\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const person = {<\/p>\n<p>  name: &#039;Alex&#039;,<\/p>\n<p>  greet: function() {<\/p>\n<p>    \/\/ \u6b63\u786e\u5730\u6307\u5411person\u5bf9\u8c61<\/p>\n<p>    console.log(`Hello, my name is ${this.name}`);<\/p>\n<p>  }<\/p>\n<p>};<\/p>\n<p>person.greet(); \/\/ \u6b63\u786e\u8f93\u51fa\uff1aHello, my name is Alex<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u800c\u5982\u679c\u6211\u4eec\u4f7f\u7528\u7bad\u5934\u51fd\u6570\uff0c\u5219\u53ef\u80fd\u4e0d\u4f1a\u5f97\u5230\u9884\u671f\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const person = {<\/p>\n<p>  name: &#039;Alex&#039;,<\/p>\n<p>  greet: () =&gt; {<\/p>\n<p>    \/\/ &#039;this&#039; \u4e0d\u6307\u5411person\u5bf9\u8c61<\/p>\n<p>    console.log(`Hello, my name is ${this.name}`);<\/p>\n<p>  }<\/p>\n<p>};<\/p>\n<p>person.greet(); \/\/ \u8f93\u51fa\uff1aHello, my name is \uff0c\u56e0\u4e3athis\u6ca1\u6709\u7ed1\u5b9a\u5230person\u4e0a<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56e0\u6b64\uff0c\u5c3d\u7ba1\u7bad\u5934\u51fd\u6570\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u6211\u4eec\u4e5f\u5fc5\u987b\u6ce8\u610f\u5b83\u4eec\u5bf9\u4e0a\u4e0b\u6587<code>this<\/code>\u7684\u5f71\u54cd\uff0c\u4ee5\u53ca\u5b83\u4eec\u7684\u4f7f\u7528\u9650\u5236\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c\u7bad\u5934\u51fd\u6570\u662fJavaScript ES6\u4e2d\u5f15\u5165\u7684\u4e00\u4e2a\u5f3a\u5927\u7279\u6027\uff0c\u5b83\u4e0d\u4ec5\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\uff0c\u4e5f\u8ba9\u51fd\u6570\u4f5c\u7528\u57df\u94fe\u4e2d\u7684<code>this<\/code>\u5904\u7406\u53d8\u5f97\u66f4\u5bb9\u6613\u3002\u7136\u800c\uff0c\u4e86\u89e3\u7bad\u5934\u51fd\u6570\u7684\u9002\u7528\u573a\u666f\u548c\u5c40\u9650\u6027\u5bf9\u4e8e\u7f16\u5199\u53ef\u9760\u4e14\u53ef\u7ef4\u62a4\u7684\u4ee3\u7801\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>JavaScript\u4e2d\u7bad\u5934\u51fd\u6570\u7684\u7528\u6cd5\u6709\u54ea\u4e9b\uff1f<\/strong><\/p>\n<p>\u7bad\u5934\u51fd\u6570\u662fES6\u5f15\u5165\u7684\u4e00\u79cd\u65b0\u7684\u51fd\u6570\u58f0\u660e\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7b80\u5316\u51fd\u6570\u7684\u7f16\u5199\u548c\u4e66\u5199\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u7684\u5e38\u89c1\u65b9\u5f0f\uff1a<\/p>\n<ol>\n<li>\n<p>\u7b80\u5355\u7684\u51fd\u6570\u8868\u8fbe\u5f0f\uff1a\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u53ef\u4ee5\u66f4\u52a0\u7b80\u6d01\u5730\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u4f8b\u5982\uff1a<code>(x) =&gt; x * x<\/code>\u8868\u793a\u4e00\u4e2a\u63a5\u53d7\u53c2\u6570x\u5e76\u8fd4\u56dex\u7684\u5e73\u65b9\u7684\u51fd\u6570\u3002<\/p>\n<\/li>\n<li>\n<p>\u9690\u5f0f\u8fd4\u56de\u503c\uff1a\u6ca1\u6709\u51fd\u6570\u4f53\u5757\u7684\u7bad\u5934\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7684\u503c\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528return\u8bed\u53e5\u3002\u4f8b\u5982\uff1a<code>x =&gt; x * x<\/code>\u8868\u793a\u4e00\u4e2a\u63a5\u53d7\u53c2\u6570x\u5e76\u8fd4\u56dex\u7684\u5e73\u65b9\u7684\u51fd\u6570\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f5c\u4e3a\u56de\u8c03\u51fd\u6570\uff1a\u5728\u5f02\u6b65\u7f16\u7a0b\u4e2d\uff0c\u7bad\u5934\u51fd\u6570\u7ecf\u5e38\u88ab\u7528\u4f5c\u56de\u8c03\u51fd\u6570\uff0c\u4f8b\u5982\u5728setTimeout\u6216\u8005forEach\u7b49\u51fd\u6570\u4e2d\u3002\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u53ef\u4ee5\u907f\u514dthis\u6307\u5411\u7684\u95ee\u9898\u3002<\/p>\n<\/li>\n<li>\n<p>\u7bad\u5934\u51fd\u6570\u4e0e\u8bcd\u6cd5\u4f5c\u7528\u57df\uff1a\u7bad\u5934\u51fd\u6570\u7ee7\u627f\u4e86\u6240\u5728\u4e0a\u4e0b\u6587\u7684\u8bcd\u6cd5\u4f5c\u7528\u57df\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4eec\u7684this\u5bf9\u8c61\u662f\u5728\u5b9a\u4e49\u51fd\u6570\u65f6\u786e\u5b9a\u7684\uff0c\u800c\u4e0d\u662f\u5728\u8c03\u7528\u51fd\u6570\u65f6\u786e\u5b9a\u7684\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e0e\u5176\u4ed6\u8bed\u8a00\u7c7b\u4f3c\uff1a\u5982\u679c\u4f60\u719f\u6089\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u7bad\u5934\u51fd\u6570\uff0c\u4f60\u4f1a\u53d1\u73b0JavaScript\u4e2d\u7684\u7bad\u5934\u51fd\u6570\u7528\u6cd5\u4e0e\u4e4b\u76f8\u4f3c\uff0c\u4f8b\u5982Python\u4e2d\u7684Lambda\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u5982\u4f55\u5728JavaScript\u4e2d\u4f20\u9012\u7bad\u5934\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff1f<\/strong><\/p>\n<p>\u5728JavaScript\u4e2d\uff0c\u7bad\u5934\u51fd\u6570\u53ef\u4ee5\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u4f5c\u4e3a\u56de\u8c03\u51fd\u6570\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-javascript\">function doSomething(callback) {\n  \/\/ \u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\n  \/\/ \u56de\u8c03\u51fd\u6570\u5728\u8fd9\u91cc\u88ab\u8c03\u7528\n  callback();\n}\n\n\/\/ \u4f20\u9012\u7bad\u5934\u51fd\u6570\u4f5c\u4e3a\u56de\u8c03\u51fd\u6570\ndoSomething(() =&gt; {\n  console.log(&quot;\u7bad\u5934\u51fd\u6570\u4f5c\u4e3a\u56de\u8c03\u51fd\u6570\u88ab\u8c03\u7528\u4e86&quot;);\n});\n<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>doSomething<\/code>\u7684\u51fd\u6570\uff0c\u5e76\u5c06\u7bad\u5934\u51fd\u6570\u4f5c\u4e3a<code>callback<\/code>\u53c2\u6570\u4f20\u9012\u7ed9\u5b83\u3002\u5f53<code>doSomething<\/code>\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u7bad\u5934\u51fd\u6570\u4f1a\u88ab\u6267\u884c\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u6765\u6539\u53d8this\u6307\u5411\uff1f<\/strong><\/p>\n<p>\u5728\u4f20\u7edf\u7684JavaScript\u51fd\u6570\u4e2d\uff0cthis\u7684\u503c\u5728\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u624d\u4f1a\u786e\u5b9a\uff0c\u800c\u5728\u7bad\u5934\u51fd\u6570\u4e2d\uff0cthis\u7684\u503c\u5728\u51fd\u6570\u5b9a\u4e49\u65f6\u5df2\u7ecf\u786e\u5b9a\u4e86\uff0c\u5e76\u4e14\u662f\u7ee7\u627f\u81ea\u5916\u5c42\u4f5c\u7528\u57df\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u6765\u6539\u53d8this\u6307\u5411\uff1a<\/p>\n<pre><code class=\"language-javascript\">const obj = {\n  name: &quot;JavaScript&quot;,\n  sayHello: function() {\n    \/\/ \u4f7f\u7528\u7bad\u5934\u51fd\u6570\u6539\u53d8this\u6307\u5411\n    const greet = () =&gt; {\n      console.log(`Hello, ${this.name}!`);\n    };\n\n    greet();\n  }\n};\n\nobj.sayHello();  \/\/ \u8f93\u51fa: &quot;Hello, JavaScript!&quot;\n<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7bad\u5934\u51fd\u6570<code>greet<\/code>\u7ee7\u627f\u4e86\u5916\u5c42\u4f5c\u7528\u57df\u7684this\u503c\uff0c\u56e0\u6b64\u5728\u7bad\u5934\u51fd\u6570\u5185\u90e8\u7684this\u6307\u5411\u4ecd\u7136\u662fobj\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u5168\u5c40\u5bf9\u8c61\u3002\u8fd9\u662f\u7bad\u5934\u51fd\u6570\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u7279\u6027\uff0c\u53ef\u4ee5\u907f\u514d\u4f20\u7edf\u7684\u51fd\u6570\u4e2dthis\u6307\u5411\u7684\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f53\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u65f6\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u5199\u51fd\u6570\u8868\u8fbe\u5f0f\u3001\u6ca1\u6709\u81ea\u5df1\u7684this\u3001arguments\u3001super [&hellip;]","protected":false},"author":3,"featured_media":154275,"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\/154271"}],"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=154271"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/154271\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/154275"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=154271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=154271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=154271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}