{"id":168849,"date":"2024-05-08T16:32:53","date_gmt":"2024-05-08T08:32:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/168849.html"},"modified":"2024-05-08T16:33:00","modified_gmt":"2024-05-08T08:33:00","slug":"javascript-%e9%a1%b9%e7%9b%ae%e4%b8%ad%e7%9a%84%e5%9b%9e%e8%b0%83%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/168849.html","title":{"rendered":"JavaScript \u9879\u76ee\u4e2d\u7684\u56de\u8c03\u51fd\u6570\u5982\u4f55\u7528"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27035034\/34cdc6eb-6ae1-4a0f-8a8e-ccbbabe06bf2.webp\" alt=\"JavaScript \u9879\u76ee\u4e2d\u7684\u56de\u8c03\u51fd\u6570\u5982\u4f55\u7528\" \/><\/p>\n<p><p>\u5728JavaScript\u9879\u76ee\u4e2d\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u5e38\u89c1\u7684\u5f02\u6b65\u7f16\u7a0b\u6280\u672f\u3002<strong>\u56de\u8c03\u51fd\u6570\u662f\u4e00\u79cd\u4ee5\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u5728\u9002\u5f53\u7684\u65f6\u5019\u88ab\u8c03\u7528\u4ee5\u5b8c\u6210\u67d0\u9879\u4efb\u52a1\u7684\u51fd\u6570<\/strong>\u3002\u8fd9\u79cd\u505a\u6cd5\u5728\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u5982\u7f51\u7edc\u8bf7\u6c42\u3001\u6587\u4ef6\u8bfb\u5199\u7b49\u573a\u666f\u5c24\u4e3a\u5e38\u89c1\uff0c<strong>\u5141\u8bb8\u6211\u4eec\u6709\u6548\u63a7\u5236\u5f02\u6b65\u64cd\u4f5c\u7684\u6267\u884c\u65f6\u95f4\u70b9\u4e0e\u987a\u5e8f\u3001\u63d0\u9ad8\u4ee3\u7801\u7684\u6a21\u5757\u5316\u4e0e\u91cd\u7528\u6027\uff0c\u4ee5\u53ca\u4f18\u5316\u7528\u6237\u4f53\u9a8c<\/strong>\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728JavaScript\u9879\u76ee\u4e2d\u6709\u6548\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u65f6\uff0c\u6700\u4e3a\u6838\u5fc3\u7684\u4e00\u4e2a\u6982\u5ff5\u662f<strong>\u9ad8\u9636\u51fd\u6570<\/strong>\uff08Higher-Order Function\uff09\u3002\u8fd9\u7c7b\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u5176\u4ed6\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u6216\u8005\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\u3002\u5b83\u4eec\u4f7f\u5f97\u56de\u8c03\u51fd\u6570\u7684\u5e94\u7528\u6210\u4e3a\u53ef\u80fd\u3002\u4f8b\u5982\uff0cJavaScript\u6570\u7ec4\u7684<code>.forEach<\/code>\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u8be5\u51fd\u6570\uff0c\u8fd9\u5c31\u662f\u4e00\u4e2a\u5178\u578b\u7684\u9ad8\u9636\u51fd\u6570\u5e94\u7528\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7406\u89e3\u56de\u8c03\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5bf9\u5176\u6709\u4e00\u4e2a\u6e05\u6670\u7684\u7406\u89e3\u3002\u56de\u8c03\u51fd\u6570\u662f\u5728JavaScript\u5f02\u6b65\u7f16\u7a0b\u6a21\u578b\u4e2d\u5e94\u7528\u5e7f\u6cdb\u7684\u6982\u5ff5\u3002\u901a\u8fc7\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u5728\u540e\u8005\u5185\u90e8\u5728\u9002\u5f53\u7684\u65f6\u673a\u8c03\u7528\uff0c\u6211\u4eec\u80fd\u591f\u5728\u4fdd\u6301\u4ee3\u7801\u7ed3\u6784\u6e05\u6670\u7684\u540c\u65f6\uff0c\u6709\u6548\u7ba1\u7406\u5f02\u6b65\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u4e2a\u7b80\u5355\u7684\u56de\u8c03\u51fd\u6570\u793a\u4f8b\u53ef\u80fd\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function greeting(name) {<\/p>\n<p>    console.log(&#039;Hello &#039; + name);<\/p>\n<p>}<\/p>\n<p>function processUserInput(callback) {<\/p>\n<p>    var name = prompt(&#039;Please enter your name.&#039;);<\/p>\n<p>    callback(name);<\/p>\n<p>}<\/p>\n<p>processUserInput(greeting);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>processUserInput<\/code>\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u51fd\u6570\uff08\u56de\u8c03\u51fd\u6570\uff09\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5728\u7528\u6237\u8f93\u5165\u5b8c\u6210\u540e\u8c03\u7528\u5b83\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u5904\u7406\u5f02\u6b65\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5f02\u6b65\u64cd\u4f5c\uff0c\u4f8b\u5982\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u6570\u636e\u6216\u8bfb\u53d6\u672c\u5730\u6587\u4ef6\uff0c\u662f\u56de\u8c03\u51fd\u6570\u5e94\u7528\u7684\u4e00\u4e2a\u91cd\u8981\u573a\u666f\u3002\u5728\u8fd9\u7c7b\u64cd\u4f5c\u4e2d\uff0c\u64cd\u4f5c\u7684\u5b8c\u6210\u65f6\u95f4\u4e0d\u786e\u5b9a\uff0c\u901a\u8fc7\u56de\u8c03\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u6267\u884c\u7279\u5b9a\u7684\u4ee3\u7801\u903b\u8f91\uff0c\u800c\u4e0d\u9700\u8981\u963b\u585e\u7a0b\u5e8f\u7684\u5176\u4ed6\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u4e2a\u5904\u7406\u7f51\u7edc\u8bf7\u6c42\u7684\u56de\u8c03\u51fd\u6570\u793a\u4f8b\u53ef\u80fd\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function fetchData(url, callback) {<\/p>\n<p>    fetch(url)<\/p>\n<p>        .then(response =&gt; response.json())<\/p>\n<p>        .then(data =&gt; callback(data))<\/p>\n<p>        .catch(error =&gt; console.error(&#039;Error:&#039;, error));<\/p>\n<p>}<\/p>\n<p>fetchData(&#039;https:\/\/api.example.com\/data&#039;, function(data) {<\/p>\n<p>    console.log(data);<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>fetchData<\/code>\u51fd\u6570\u63a5\u53d7\u4e00\u4e2aURL\u548c\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u3002\u5b83\u4f7f\u7528<code>fetch<\/code> API\u4ece\u670d\u52a1\u5668\u8bf7\u6c42\u6570\u636e\uff0c\u4e00\u65e6\u6570\u636e\u6210\u529f\u83b7\u53d6\u5e76\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\uff0c\u5219\u8c03\u7528\u63d0\u4f9b\u7684\u56de\u8c03\u51fd\u6570\u5e76\u5c06\u6570\u636e\u4f20\u9012\u7ed9\u5b83\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u56de\u8c03\u51fd\u6570\u4e0e\u9519\u8bef\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u8fdb\u884c\u5f02\u6b65\u64cd\u4f5c\u65f6\uff0c\u8003\u8651\u5230\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\u548c\u5f02\u5e38\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u901a\u5e38\u7684\u505a\u6cd5\u662f\u5728\u56de\u8c03\u51fd\u6570\u4e2d\u589e\u52a0\u4e00\u4e2a\u4e13\u7528\u4e8e\u9519\u8bef\u5904\u7406\u7684\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u4e2a\u7ed3\u5408\u9519\u8bef\u5904\u7406\u7684\u56de\u8c03\u51fd\u6570\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function fetchData(url, callback) {<\/p>\n<p>    fetch(url)<\/p>\n<p>        .then(response =&gt; {<\/p>\n<p>            if (!response.ok) {<\/p>\n<p>                throw new Error(&#039;Network response was not ok&#039;);<\/p>\n<p>            }<\/p>\n<p>            return response.json();<\/p>\n<p>        })<\/p>\n<p>        .then(data =&gt; callback(null, data))<\/p>\n<p>        .catch(error =&gt; callback(error, null));<\/p>\n<p>}<\/p>\n<p>fetchData(&#039;https:\/\/api.example.com\/data&#039;, function(error, data) {<\/p>\n<p>    if (error) {<\/p>\n<p>        console.error(&#039;Fetch Error:&#039;, error);<\/p>\n<p>    } else {<\/p>\n<p>        console.log(data);<\/p>\n<p>    }<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>fetchData<\/code>\u51fd\u6570\u7684\u56de\u8c03\u73b0\u5728\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u4e00\u4e2a\u9519\u8bef\u5bf9\u8c61\u548c\u6570\u636e\u3002\u8fd9\u4f7f\u5f97\u5728\u56de\u8c03\u51fd\u6570\u5185\u90e8\u53ef\u4ee5\u5bf9\u9519\u8bef\u548c\u6570\u636e\u8fdb\u884c\u68c0\u67e5\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u56de\u8c03\u51fd\u6570\u7684\u95ee\u9898\u548c\u66ff\u4ee3\u65b9\u6848<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u56de\u8c03\u51fd\u6570\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\uff0c\u4f46\u5b83\u4eec\u5728\u7ba1\u7406\u590d\u6742\u7684\u5f02\u6b65\u903b\u8f91\u65f6\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6240\u8c13\u7684\u201c\u56de\u8c03\u5730\u72f1\u201d\uff0c\u8fd9\u662f\u7531\u4e8e\u591a\u5c42\u5d4c\u5957\u7684\u56de\u8c03\u51fd\u6570\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u7406\u89e3\u548c\u7ef4\u62a4\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u73b0\u4ee3JavaScript\u5f15\u5165\u4e86<code>Promise<\/code>\u548c<code>async\/aw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t<\/code>\u8bed\u6cd5\uff0c\u4ee5\u66f4\u7b80\u6d01\u548c\u76f4\u89c2\u7684\u65b9\u5f0f\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>Promise<\/code>\u91cd\u6784\u4e0a\u4e00\u4e2a\u7f51\u7edc\u8bf7\u6c42\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function fetchData(url) {<\/p>\n<p>    return fetch(url)<\/p>\n<p>        .then(response =&gt; {<\/p>\n<p>            if (!response.ok) {<\/p>\n<p>                throw new Error(&#039;Network response was not ok&#039;);<\/p>\n<p>            }<\/p>\n<p>            return response.json();<\/p>\n<p>        });<\/p>\n<p>}<\/p>\n<p>fetchData(&#039;https:\/\/api.example.com\/data&#039;)<\/p>\n<p>    .then(data =&gt; console.log(data))<\/p>\n<p>    .catch(error =&gt; console.error(&#039;Fetch Error:&#039;, error));<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7248\u672c\u4e2d\uff0c\u901a\u8fc7\u8fd4\u56de\u4e00\u4e2a<code>Promise<\/code>\u5bf9\u8c61\u5e76\u4f7f\u7528<code>.then<\/code>\u548c<code>.catch<\/code>\u65b9\u6cd5\u94fe\uff0c\u6211\u4eec\u907f\u514d\u4e86\u56de\u8c03\u51fd\u6570\u7684\u76f4\u63a5\u4f7f\u7528\uff0c\u540c\u65f6\u4e5f\u4f7f\u5f97\u9519\u8bef\u5904\u7406\u53d8\u5f97\u66f4\u52a0\u6e05\u6670\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u56de\u8c03\u51fd\u6570\u662fJavaScript\u4e2d\u4e00\u79cd\u91cd\u8981\u7684\u5f02\u6b65\u7f16\u7a0b\u6280\u672f\u3002\u5b83\u4eec\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u4fdd\u6301\u4ee3\u7801\u7684\u975e\u963b\u585e\u6027\u7684\u540c\u65f6\uff0c\u6709\u6548\u7ba1\u7406\u548c\u7ec4\u7ec7\u5f02\u6b65\u64cd\u4f5c\u3002\u7136\u800c\uff0c\u5728\u5904\u7406\u590d\u6742\u7684\u5f02\u6b65\u6d41\u7a0b\u65f6\uff0c\u5e94\u4f18\u5148\u8003\u8651\u4f7f\u7528<code>Promise<\/code>\u548c<code>async\/await<\/code>\u7b49\u73b0\u4ee3JavaScript\u7279\u6027\uff0c\u4ee5\u5b9e\u73b0\u66f4\u7b80\u6d01\u3001\u66f4\u6613\u7ef4\u62a4\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7406\u89e3\u5e76\u6b63\u786e\u4f7f\u7528\u56de\u8c03\u51fd\u6570\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u81ea\u5df1\u7684\u9879\u76ee\u4e2d\u66f4\u6709\u6548\u5730\u5904\u7406\u5f02\u6b65\u903b\u8f91\uff0c\u63d0\u9ad8\u5e94\u7528\u7684\u6027\u80fd\u4e0e\u7528\u6237\u4f53\u9a8c\u3002\u540c\u65f6\uff0c\u5bf9\u4e8e\u590d\u6742\u7684\u5f02\u6b65\u64cd\u4f5c\u94fe\uff0c\u638c\u63e1\u66f4\u73b0\u4ee3\u7684\u5f02\u6b65\u5904\u7406\u6a21\u5f0f\u5c06\u662f\u63d0\u5347\u5f00\u53d1\u6548\u7387\u4e0e\u4ee3\u7801\u8d28\u91cf\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. <\/strong>\u5982\u4f55\u5728JavaScript\u9879\u76ee\u4e2d\u4f7f\u7528\u56de\u8c03\u51fd\u6570\uff1f<\/p>\n<p>\u56de\u8c03\u51fd\u6570\u662fJavaScript\u4e2d\u7684\u4e00\u79cd\u5e38\u89c1\u7f16\u7a0b\u6a21\u5f0f\uff0c\u7528\u4e8e\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u6216\u5728\u4e8b\u4ef6\u53d1\u751f\u540e\u6267\u884c\u7279\u5b9a\u7684\u4ee3\u7801\u3002\u60a8\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u5728JavaScript\u9879\u76ee\u4e2d\u4f7f\u7528\u56de\u8c03\u51fd\u6570\uff1a<\/p>\n<ul>\n<li>\u9996\u5148\uff0c\u786e\u5b9a\u9700\u8981\u6267\u884c\u7684\u5f02\u6b65\u64cd\u4f5c\u6216\u4e8b\u4ef6\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u56de\u8c03\u51fd\u6570\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5f02\u6b65\u64cd\u4f5c\u6216\u4e8b\u4ef6\u5904\u7406\u7684\u51fd\u6570\u3002<\/li>\n<li>\u5728\u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210\u6216\u4e8b\u4ef6\u53d1\u751f\u540e\uff0c\u901a\u8fc7\u8c03\u7528\u56de\u8c03\u51fd\u6570\u6765\u6267\u884c\u7279\u5b9a\u7684\u4ee3\u7801\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u5f02\u6b65\u7684AJAX\u8bf7\u6c42\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u4f7f\u7528\u56de\u8c03\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"language-javascript\">function fetchData(callback) {\n  \/\/ \u5f02\u6b65\u64cd\u4f5c\u6216AJAX\u8bf7\u6c42\n  \/\/ \u5b8c\u6210\u540e\u8c03\u7528\u56de\u8c03\u51fd\u6570\n  callback(data);\n}\n\nfunction handleData(data) {\n  \/\/ \u5904\u7406\u6570\u636e\u7684\u4ee3\u7801\n}\n\nfetchData(handleData);\n<\/code><\/pre>\n<p><strong>2. <\/strong>\u5982\u4f55\u4f20\u9012\u53c2\u6570\u7ed9JavaScript\u9879\u76ee\u4e2d\u7684\u56de\u8c03\u51fd\u6570\uff1f<\/p>\n<p>\u5728JavaScript\u9879\u76ee\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u4f20\u9012\u53c2\u6570\u7ed9\u56de\u8c03\u51fd\u6570\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u533f\u540d\u51fd\u6570\uff1a\u901a\u8fc7\u5728\u8c03\u7528\u56de\u8c03\u51fd\u6570\u65f6\u5b9a\u4e49\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff0c\u5e76\u5728\u5176\u4e2d\u4f20\u9012\u53c2\u6570\u3002<\/li>\n<li>\u4f7f\u7528bind()\u65b9\u6cd5\uff1a\u4f7f\u7528bind()\u65b9\u6cd5\u5c06\u9700\u8981\u4f20\u9012\u7684\u53c2\u6570\u7ed1\u5b9a\u5230\u56de\u8c03\u51fd\u6570\u4e0a\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528\u533f\u540d\u51fd\u6570\u548cbind()\u65b9\u6cd5\u4f20\u9012\u53c2\u6570\u7ed9\u56de\u8c03\u51fd\u6570\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u4f7f\u7528\u533f\u540d\u51fd\u6570\u4f20\u9012\u53c2\u6570\nfunction fetchData(callback) {\n  var data = &#039;hello world&#039;;\n  callback(data);\n}\n\nfetchData(function(data) {\n  console.log(data); \/\/ \u8f93\u51fa &#039;hello world&#039;\n});\n\n\/\/ \u4f7f\u7528bind()\u65b9\u6cd5\u4f20\u9012\u53c2\u6570\nfunction fetchData(callback) {\n  var data = &#039;hello world&#039;;\n  callback(data);\n}\n\nfunction handleData(data, param) {\n  console.log(data, param); \/\/ \u8f93\u51fa &#039;hello world&#039;, &#039;extra param&#039;\n}\n\nfetchData(handleData.bind(null, &#039;extra param&#039;));\n<\/code><\/pre>\n<p><strong>3. <\/strong>\u5982\u4f55\u5904\u7406\u56de\u8c03\u5730\u72f1\u95ee\u9898\uff0c\u5728JavaScript\u9879\u76ee\u4e2d\u6709\u6548\u7ba1\u7406\u591a\u4e2a\u56de\u8c03\u51fd\u6570\uff1f<\/p>\n<p>\u56de\u8c03\u5730\u72f1\u6307\u7684\u662f\u5f53\u5b58\u5728\u591a\u4e2a\u5d4c\u5957\u7684\u56de\u8c03\u51fd\u6570\u65f6\uff0c\u4ee3\u7801\u53d8\u5f97\u96be\u4ee5\u9605\u8bfb\u548c\u7ef4\u62a4\u7684\u95ee\u9898\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5e76\u5728JavaScript\u9879\u76ee\u4e2d\u6709\u6548\u7ba1\u7406\u591a\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u63aa\u65bd\uff1a<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528Promise\u5bf9\u8c61\uff1aPromise\u5bf9\u8c61\u662f\u4e00\u79cd\u7528\u4e8e\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u7684\u673a\u5236\uff0c\u53ef\u4ee5\u907f\u514d\u56de\u8c03\u5730\u72f1\u7684\u95ee\u9898\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528Promise\u5bf9\u8c61\u6765\u7ba1\u7406\u591a\u4e2a\u5f02\u6b65\u64cd\u4f5c\uff0c\u5e76\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u6267\u884c\u7279\u5b9a\u7684\u4ee3\u7801\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528async\/await\uff1aasync\/await\u662fES2017\u5f15\u5165\u7684\u8bed\u6cd5\u7cd6\uff0c\u53ef\u4ee5\u4f7f\u5f02\u6b65\u4ee3\u7801\u770b\u8d77\u6765\u50cf\u540c\u6b65\u4ee3\u7801\u4e00\u6837\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528async\/await\u6765\u5904\u7406\u591a\u4e2a\u5f02\u6b65\u64cd\u4f5c\uff0c\u5e76\u6309\u7167\u987a\u5e8f\u6267\u884c\u4ee3\u7801\uff0c\u800c\u4e0d\u9700\u8981\u5d4c\u5957\u7684\u56de\u8c03\u51fd\u6570\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u8fd9\u91cc\u662f\u4f7f\u7528Promise\u548casync\/await\u5904\u7406\u591a\u4e2a\u56de\u8c03\u51fd\u6570\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u4f7f\u7528Promise\u5904\u7406\u591a\u4e2a\u56de\u8c03\u51fd\u6570\nfetchData()\n  .then(function(data) {\n    return processData(data);\n  })\n  .then(function(result) {\n    console.log(result);\n  })\n  .catch(function(error) {\n    console.error(error);\n  });\n\n\/\/ \u4f7f\u7528async\/await\u5904\u7406\u591a\u4e2a\u56de\u8c03\u51fd\u6570\nasync function fetchDataAndProcess() {\n  try {\n    var data = await fetchData();\n    var result = await processData(data);\n    console.log(result);\n  } catch (error) {\n    console.error(error);\n  }\n}\n\nfetchDataAndProcess();\n<\/code><\/pre>\n<p>\u901a\u8fc7\u4f7f\u7528Promise\u548casync\/await\uff0c\u60a8\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u548c\u5904\u7406\u591a\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u5e76\u4f7f\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u548c\u5bb9\u6613\u7406\u89e3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728JavaScript\u9879\u76ee\u4e2d\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u5e38\u89c1\u7684\u5f02\u6b65\u7f16\u7a0b\u6280\u672f\u3002\u56de\u8c03\u51fd\u6570\u662f\u4e00\u79cd\u4ee5\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00 [&hellip;]","protected":false},"author":3,"featured_media":168863,"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\/168849"}],"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=168849"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/168849\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/168863"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=168849"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=168849"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=168849"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}