{"id":2081,"date":"2025-02-26T23:41:26","date_gmt":"2025-02-26T15:41:26","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2081"},"modified":"2025-02-26T23:41:26","modified_gmt":"2025-02-26T15:41:26","slug":"javascript-%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/26\/javascript-%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b\/","title":{"rendered":"JavaScript \u5f02\u6b65\u7f16\u7a0b"},"content":{"rendered":"\n<p>JavaScript \u662f\u4e00\u95e8\u5355\u7ebf\u7a0b\u7f16\u7a0b\u8bed\u8a00\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4e00\u6b21\u53ea\u80fd\u6267\u884c\u4e00\u4e2a\u4efb\u52a1\u3002\u7136\u800c\uff0c\u5728\u73b0\u4ee3\u5e94\u7528\u4e2d\uff0c\u8bb8\u591a\u64cd\u4f5c\uff08\u5982\u7f51\u7edc\u8bf7\u6c42\u3001\u6587\u4ef6\u8bfb\u53d6\u7b49\uff09\u662f\u5f02\u6b65\u7684\u3002\u5f02\u6b65\u7f16\u7a0b\u4f7f\u5f97 JavaScript \u53ef\u4ee5\u5728\u7b49\u5f85\u8fd9\u4e9b\u64cd\u4f5c\u5b8c\u6210\u65f6\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u4efb\u52a1\uff0c\u4ece\u800c\u907f\u514d\u963b\u585e\u3002<\/p>\n\n\n\n<p>JavaScript \u63d0\u4f9b\u4e86\u51e0\u79cd\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u7684\u673a\u5236\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u56de\u8c03\u51fd\u6570\uff08Callback\uff09<\/strong><\/h3>\n\n\n\n<p>\u56de\u8c03\u51fd\u6570\u662f\u4e00\u79cd\u6700\u5e38\u89c1\u7684\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u7684\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u5c06\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u540e\u8005\u5728\u5b8c\u6210\u5f02\u6b65\u64cd\u4f5c\u540e\u8c03\u7528\u8be5\u56de\u8c03\u51fd\u6570\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>function fetchData(callback) {\n  setTimeout(function() {\n    console.log(\"Data fetched\");\n    callback(\"Here is your data\");\n  }, 1000);\n}\n\nfetchData(function(data) {\n  console.log(data);  \/\/ \u8f93\u51fa \"Here is your data\"\n});<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>fetchData<\/code> \u51fd\u6570\u6a21\u62df\u4e86\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\uff0c1\u79d2\u949f\u540e\u8c03\u7528\u4f20\u5165\u7684\u56de\u8c03\u51fd\u6570\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u95ee\u9898\uff1a<\/strong><\/h5>\n\n\n\n<p>\u56de\u8c03\u5730\u72f1\uff08Callback Hell\uff09\u662f\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u65f6\u7684\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\u3002\u5f53\u591a\u4e2a\u5f02\u6b65\u64cd\u4f5c\u5d4c\u5957\u65f6\uff0c\u4ee3\u7801\u4f1a\u53d8\u5f97\u96be\u4ee5\u9605\u8bfb\u548c\u7ef4\u62a4\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Promise<\/strong><\/h3>\n\n\n\n<p>Promise \u662f JavaScript \u4e2d\u66f4\u73b0\u4ee3\u7684\u5f02\u6b65\u7f16\u7a0b\u89e3\u51b3\u65b9\u6848\uff0c\u5b83\u63d0\u4f9b\u4e86\u6bd4\u56de\u8c03\u51fd\u6570\u66f4\u6e05\u6670\u7684\u5f02\u6b65\u64cd\u4f5c\u5904\u7406\u65b9\u5f0f\u3002Promise \u662f\u4e00\u4e2a\u8868\u793a\u5f02\u6b65\u64cd\u4f5c\u6700\u7ec8\u5b8c\u6210\u6216\u8005\u5931\u8d25\u7684\u5bf9\u8c61\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Pending\uff08\u5f85\u5b9a\uff09\uff1a<\/strong> Promise \u521d\u59cb\u5316\u65f6\u7684\u72b6\u6001\uff0c\u8868\u793a\u5f02\u6b65\u64cd\u4f5c\u6b63\u5728\u8fdb\u884c\u3002<\/li>\n\n\n\n<li><strong>Resolved\uff08\u5df2\u5b8c\u6210\uff09\uff1a<\/strong> Promise \u6210\u529f\u5b8c\u6210\u64cd\u4f5c\uff0c\u4e14\u63d0\u4f9b\u4e86\u7ed3\u679c\u503c\u3002<\/li>\n\n\n\n<li><strong>Rejected\uff08\u5df2\u5931\u8d25\uff09\uff1a<\/strong> Promise \u5931\u8d25\u5e76\u8fd4\u56de\u4e00\u4e2a\u9519\u8bef\u539f\u56e0\u3002<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>function fetchData() {\n  return new Promise(function(resolve, reject) {\n    setTimeout(function() {\n      console.log(\"Data fetched\");\n      resolve(\"Here is your data\");\n    }, 1000);\n  });\n}\n\nfetchData()\n  .then(function(data) {\n    console.log(data);  \/\/ \u8f93\u51fa \"Here is your data\"\n  })\n  .catch(function(error) {\n    console.log(error);\n  });<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>fetchData<\/code> \u8fd4\u56de\u4e86\u4e00\u4e2a Promise\u3002\u5f53 Promise \u5b8c\u6210\u65f6\uff0c\u8c03\u7528 <code>.then()<\/code> \u65b9\u6cd5\u5904\u7406\u6210\u529f\u7684\u7ed3\u679c\uff0c\u6216\u8005\u4f7f\u7528 <code>.catch()<\/code> \u5904\u7406\u9519\u8bef\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u4f18\u70b9\uff1a<\/strong><\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u5f02\u6b65\u64cd\u4f5c\u7684\u987a\u5e8f\u548c\u9519\u8bef\u5904\u7406\u53d8\u5f97\u66f4\u52a0\u6e05\u6670\u3002<\/li>\n\n\n\n<li>\u53ef\u4ee5\u901a\u8fc7 <code>.then()<\/code> \u94fe\u63a5\u591a\u4e2a\u5f02\u6b65\u64cd\u4f5c\uff0c\u907f\u514d\u56de\u8c03\u5730\u72f1\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Async \/ Await<\/strong><\/h3>\n\n\n\n<p><code>async<\/code> \u548c <code>await<\/code> \u662f ES8\uff08ECMAScript 2017\uff09\u5f15\u5165\u7684\u8bed\u6cd5\u7cd6\uff0c\u4f7f\u5f97\u5f02\u6b65\u4ee3\u7801\u770b\u8d77\u6765\u66f4\u50cf\u662f\u540c\u6b65\u4ee3\u7801\u3002<code>async<\/code> \u4fee\u9970\u51fd\u6570\uff0c\u8868\u793a\u8be5\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a Promise\uff0c\u800c <code>await<\/code> \u7528\u4e8e\u7b49\u5f85 Promise \u5b8c\u6210\uff0c\u5e76\u53ef\u4ee5\u76f4\u63a5\u83b7\u53d6\u5176\u7ed3\u679c\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>function fetchData() {\n  return new Promise(function(resolve, reject) {\n    setTimeout(function() {\n      console.log(\"Data fetched\");\n      resolve(\"Here is your data\");\n    }, 1000);\n  });\n}\n\nasync function getData() {\n  const data = await fetchData();\n  console.log(data);  \/\/ \u8f93\u51fa \"Here is your data\"\n}\n\ngetData();<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>getData<\/code> \u662f\u4e00\u4e2a <code>async<\/code> \u51fd\u6570\uff0c\u91cc\u9762\u7684 <code>await<\/code> \u8868\u793a\u5728\u83b7\u53d6 <code>fetchData<\/code> \u8fd4\u56de\u7684 Promise \u7ed3\u679c\u4e4b\u524d\uff0c\u4ee3\u7801\u4f1a\u6682\u505c\uff0c\u7b49\u5f85\u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210\u3002<code>await<\/code> \u53ea\u80fd\u5728 <code>async<\/code> \u51fd\u6570\u5185\u90e8\u4f7f\u7528\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u4f18\u70b9\uff1a<\/strong><\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u5f02\u6b65\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u7b80\u6d01\u6027\uff0c\u907f\u514d\u4e86 <code>.then()<\/code> \u94fe\u5f0f\u8c03\u7528\u3002<\/li>\n\n\n\n<li>\u652f\u6301 <code>try<\/code> \u548c <code>catch<\/code> \u8bed\u53e5\u8fdb\u884c\u9519\u8bef\u5904\u7406\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. setTimeout \u548c setInterval<\/strong><\/h3>\n\n\n\n<p><code>setTimeout<\/code> \u548c <code>setInterval<\/code> \u662f JavaScript \u63d0\u4f9b\u7684\u4e24\u79cd\u5e38\u7528\u5b9a\u65f6\u5668\u65b9\u6cd5\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>setTimeout()<\/code><\/strong>\uff1a\u5728\u6307\u5b9a\u7684\u5ef6\u8fdf\u65f6\u95f4\u540e\u6267\u884c\u4e00\u6b21\u56de\u8c03\u51fd\u6570\u3002<\/li>\n\n\n\n<li><strong><code>setInterval()<\/code><\/strong>\uff1a\u5728\u6307\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u5185\u91cd\u590d\u6267\u884c\u56de\u8c03\u51fd\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u4f7f\u7528 setTimeout\nsetTimeout(function() {\n  console.log(\"This message is delayed by 2 seconds\");\n}, 2000);\n\n\/\/ \u4f7f\u7528 setInterval\nlet count = 0;\nconst intervalId = setInterval(function() {\n  console.log(\"This message is displayed every 1 second\");\n  count++;\n  if (count === 5) {\n    clearInterval(intervalId);  \/\/ \u505c\u6b62\u5b9a\u65f6\u5668\n  }\n}, 1000);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. \u5f02\u5e38\u5904\u7406<\/strong><\/h3>\n\n\n\n<p>\u5bf9\u4e8e\u5f02\u6b65\u64cd\u4f5c\uff0c\u9519\u8bef\u5904\u7406\u662f\u5f88\u91cd\u8981\u7684\u3002<code>Promise<\/code> \u63d0\u4f9b\u4e86 <code>.catch()<\/code> \u65b9\u6cd5\u6765\u5904\u7406\u9519\u8bef\uff0c\u800c <code>async<\/code> \/ <code>await<\/code> \u53ef\u4ee5\u7ed3\u5408 <code>try<\/code> \u548c <code>catch<\/code> \u6765\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u7684\u9519\u8bef\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>function fetchData() {\n  return new Promise((resolve, reject) =&gt; {\n    setTimeout(() =&gt; {\n      \/\/ \u8fd9\u91cc\u6a21\u62df\u9519\u8bef\u53d1\u751f\n      reject(\"Error occurred while fetching data\");\n    }, 1000);\n  });\n}\n\nasync function getData() {\n  try {\n    const data = await fetchData();\n    console.log(data);\n  } catch (error) {\n    console.log(error);  \/\/ \u8f93\u51fa \"Error occurred while fetching data\"\n  }\n}\n\ngetData();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u56de\u8c03\u51fd\u6570\uff08Callback\uff09<\/strong>\uff1a\u8f83\u8001\u7684\u5f02\u6b65\u5904\u7406\u65b9\u5f0f\uff0c\u6613\u4ea7\u751f\u56de\u8c03\u5730\u72f1\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>Promise<\/strong>\uff1a\u63d0\u4f9b\u4e86\u66f4\u6e05\u6670\u7684\u5f02\u6b65\u64cd\u4f5c\u5904\u7406\u65b9\u5f0f\uff0c\u652f\u6301\u94fe\u5f0f\u8c03\u7528\u548c\u9519\u8bef\u5904\u7406\u3002<\/li>\n\n\n\n<li><strong>Async \/ Await<\/strong>\uff1a\u73b0\u4ee3\u7684\u5f02\u6b65\u7f16\u7a0b\u8bed\u6cd5\u7cd6\uff0c\u4f7f\u5f02\u6b65\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\uff0c\u50cf\u540c\u6b65\u4ee3\u7801\u4e00\u6837\u5199\u5f02\u6b65\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li><strong>\u5b9a\u65f6\u5668\uff08setTimeout \/ setInterval\uff09<\/strong>\uff1a\u7528\u4e8e\u5728\u6307\u5b9a\u65f6\u95f4\u5ef6\u8fdf\u540e\u6267\u884c\u64cd\u4f5c\u6216\u91cd\u590d\u6267\u884c\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u5f02\u6b65\u7f16\u7a0b\u5de5\u5177\uff0cJavaScript \u53ef\u4ee5\u5728\u8fdb\u884c\u957f\u65f6\u95f4\u64cd\u4f5c\u65f6\uff0c\u4fdd\u6301\u7a0b\u5e8f\u7684\u6d41\u7545\u6267\u884c\uff0c\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript \u662f\u4e00\u95e8\u5355\u7ebf\u7a0b\u7f16\u7a0b\u8bed\u8a00\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4e00\u6b21\u53ea\u80fd\u6267\u884c\u4e00\u4e2a\u4efb\u52a1\u3002\u7136\u800c\uff0c\u5728\u73b0\u4ee3\u5e94\u7528\u4e2d\uff0c\u8bb8\u591a\u64cd\u4f5c\uff08\u5982\u7f51 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-2081","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2081"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2081\/revisions"}],"predecessor-version":[{"id":2082,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2081\/revisions\/2082"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}