{"id":7218,"date":"2022-11-29T09:20:14","date_gmt":"2022-11-29T01:20:14","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=7218"},"modified":"2023-11-28T15:58:47","modified_gmt":"2023-11-28T07:58:47","slug":"javascript-tong-bu-yi-bu","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2022\/11\/29\/javascript-tong-bu-yi-bu\/","title":{"rendered":"Javascript\u540c\u6b65\u5f02\u6b65"},"content":{"rendered":"<p>##JS\u4e09\u5ea7\u5927\u5c71<br \/>\n<img decoding=\"async\" src=\"https:\/\/ycdledu-1301142972.cos.ap-chengdu.myqcloud.com\/2020\/10\/17\/16029246035054.jpg\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/ycdledu-1301142972.cos.ap-chengdu.myqcloud.com\/2020\/10\/17\/16029247217112.jpg\" alt=\"\" \/><\/p>\n<blockquote>\n<p>JavaScript\u662f\u4e00\u95e8\u5355\u7ebf\u7a0b\u7684\u8bed\u8a00\uff0c\u56e0\u6b64\uff0cJavaScript\u5728\u540c\u4e00\u4e2a\u65f6\u95f4\u53ea\u80fd\u505a\u4e00\u4ef6\u4e8b\uff0c\u5355\u7ebf\u7a0b\u610f\u5473\u7740\uff0c\u5982\u679c\u5728\u540c\u4e2a\u65f6\u95f4\u6709\u591a\u4e2a\u4efb\u52a1\u7684\u8bdd\uff0c\u8fd9\u4e9b\u4efb\u52a1\u5c31\u9700\u8981\u8fdb\u884c\u6392\u961f\uff0c\u524d\u4e00\u4e2a\u4efb\u52a1\u6267\u884c\u5b8c\uff0c\u624d\u4f1a\u6267\u884c\u4e0b\u4e00\u4e2a\u4efb\u52a1<\/p>\n<\/blockquote>\n<p>###JavaScript\u7684\u5355\u7ebf\u7a0b\uff0c\u4e0e\u5b83\u7684\u7528\u9014\u662f\u6709\u5f88\u5927\u5173\u7cfb\uff0cJavaScript\u662f\u6d4f\u89c8\u5668\u7684\u811a\u672c\u8bed\u8a00<\/p>\n<p>###\u5f02\u6b65\u7684\u4ea7\u751f\u539f\u56e0<br \/>\n1\u3001\u5b9a\u65f6\u5668\u90fd\u662f\u5f02\u6b65\u64cd\u4f5c<br \/>\n2\u3001\u4e8b\u4ef6\u7ed1\u5b9a\u90fd\u662f\u5f02\u6b65\u64cd\u4f5c<br \/>\n3\u3001AJAX\u4e2d\u4e00\u822c\u6211\u4eec\u90fd\u91c7\u53d6\u5f02\u6b65\u64cd\u4f5c<br \/>\n4\u3001\u56de\u8c03\u51fd\u6570\u53ef\u4ee5\u7406\u89e3\u4e3a\u5f02\u6b65<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u540c\u6b65\u4ee3\u7801\nfunction fun1() {\n  console.log(1);\n}\nfunction fun2() {\n  console.log(2);\n}\nfun1();\nfun2();\n\n\/\/ \u8f93\u51fa\n1\n2\n\n<\/code><\/pre>\n<p>##\u8868\u767d\u7684\u6848\u4f8b<br \/>\n<img decoding=\"async\" src=\"https:\/\/ycdledu-1301142972.cos.ap-chengdu.myqcloud.com\/2020\/10\/17\/16029204098973.jpg\" alt=\"\" \/><\/p>\n<p>##\u540c\u6b65\u548c\u5f02\u6b65<\/p>\n<h2><a id=\"\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a><\/h2>\n<blockquote>\n<p>\u56e0\u4e3aJavaScript\u7684\u5355\u7ebf\u7a0b\uff0c\u56e0\u6b64\u540c\u4e2a\u65f6\u95f4\u53ea\u80fd\u5904\u7406\u540c\u4e2a\u4efb\u52a1\uff0c\u6240\u6709\u4efb\u52a1\u90fd\u9700\u8981\u6392\u961f\uff0c\u524d\u4e00\u4e2a\u4efb\u52a1\u6267\u884c\u5b8c\uff0c\u624d\u80fd\u7ee7\u7eed\u6267\u884c\u4e0b\u4e00\u4e2a\u4efb\u52a1\uff0c\u4f46\u662f\uff0c\u5982\u679c\u524d\u4e00\u4e2a\u4efb\u52a1\u7684\u6267\u884c\u65f6\u95f4\u5f88\u957f\uff0c\u6bd4\u5982\u6587\u4ef6\u7684\u8bfb\u53d6\u64cd\u4f5c\u6216ajax\u64cd\u4f5c\uff0c\u540e\u4e00\u4e2a\u4efb\u52a1\u5c31\u4e0d\u5f97\u4e0d\u7b49\u7740\uff0c\u62ffajax\u6765\u8bf4\uff0c\u5f53\u7528\u6237\u5411\u540e\u53f0\u83b7\u53d6\u5927\u91cf\u7684\u6570\u636e\u65f6\uff0c\u4e0d\u5f97\u4e0d\u7b49\u5230\u6240\u6709\u6570\u636e\u90fd\u83b7\u53d6\u5b8c\u6bd5\u624d\u80fd\u8fdb\u884c\u4e0b\u4e00\u6b65\u64cd\u4f5c\uff0c\u7528\u6237\u53ea\u80fd\u5728\u90a3\u91cc\u5e72\u7b49\u7740\uff0c\u4e25\u91cd\u5f71\u54cd\u7528\u6237\u4f53\u9a8c<\/p>\n<\/blockquote>\n<p>###\u540c\u6b65\u4efb\u52a1<br \/>\n\u540c\u6b65\u4efb\u52a1\u662f\u6307\u5728\u4e3b\u7ebf\u7a0b\u4e0a\u6392\u961f\u6267\u884c\u7684\u4efb\u52a1\uff0c\u53ea\u6709\u524d\u4e00\u4e2a\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\uff0c\u624d\u80fd\u7ee7\u7eed\u6267\u884c\u4e0b\u4e00\u4e2a\u4efb\u52a1\uff0c\u5f53\u6211\u4eec\u6253\u5f00\u7f51\u7ad9\u65f6\uff0c\u7f51\u7ad9\u7684\u6e32\u67d3\u8fc7\u7a0b\uff0c\u6bd4\u5982\u5143\u7d20\u7684\u6e32\u67d3\uff0c\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u540c\u6b65\u4efb\u52a1<\/p>\n<p>###\u5f02\u6b65\u4efb\u52a1<br \/>\n\u5f02\u6b65\u4efb\u52a1\u662f\u6307\u4e0d\u8fdb\u5165\u4e3b\u7ebf\u7a0b\uff0c\u800c\u8fdb\u5165\u4efb\u52a1\u961f\u5217\u7684\u4efb\u52a1\uff0c\u53ea\u6709\u4efb\u52a1\u961f\u5217\u901a\u77e5\u4e3b\u7ebf\u7a0b\uff0c\u67d0\u4e2a\u5f02\u6b65\u4efb\u52a1\u53ef\u4ee5\u6267\u884c\u4e86\uff0c\u8be5\u4efb\u52a1\u624d\u4f1a\u8fdb\u5165\u4e3b\u7ebf\u7a0b\uff0c\u5f53\u6211\u4eec\u6253\u5f00\u7f51\u7ad9\u65f6\uff0c\u50cf\u56fe\u7247\u7684\u52a0\u8f7d\uff0c\u97f3\u4e50\u7684\u52a0\u8f7d\uff0c\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1<\/p>\n<p>##\u5f02\u6b65<br \/>\n\u5f02\u6b65\uff08Asynchronous, async\uff09<br \/>\n\u540c\u6b65\uff08Synchronous, sync\uff09<br \/>\n<img decoding=\"async\" src=\"https:\/\/ycdledu-1301142972.cos.ap-chengdu.myqcloud.com\/2020\/10\/17\/16029169526110.png\" alt=\"\" \/><\/p>\n<pre><code class=\"language-javascript\">function fun1() {\n  console.log(1);\n}\nfunction fun2() {\n  console.log(2);\n}\nfunction fun3() {\n  console.log(3);\n}\nfun1();\nsetTimeout(function(){\n  fun2();\n},0);\nfun3();\n \n\/\/ \u8f93\u51fa\n<\/code><\/pre>\n<p>###\u5f02\u6b65\u673a\u5236<\/p>\n<ol>\n<li>\u6240\u6709\u540c\u6b65\u4efb\u52a1\u90fd\u5728\u4e3b\u7ebf\u7a0b\u4e0a\u6267\u884c\uff0c\u884c\u6210\u4e00\u4e2a\u6267\u884c\u6808<\/li>\n<li>\u4e3b\u7ebf\u7a0b\u4e4b\u5916\uff0c\u8fd8\u5b58\u5728\u4e00\u4e2a\u4efb\u52a1\u961f\u5217\uff0c\u53ea\u8981\u5f02\u6b65\u4efb\u52a1\u6709\u4e86\u7ed3\u679c\uff0c\u5c31\u4f1a\u5728\u4efb\u52a1\u961f\u5217\u4e2d\u653e\u7f6e\u4e00\u4e2a\u4e8b\u4ef6<\/li>\n<li>\u4e00\u65e6\u6267\u884c\u6808\u4e2d\u7684\u6240\u6709\u540c\u6b65\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\uff0c\u7cfb\u7edf\u5c31\u4f1a\u8bfb\u53d6\u4efb\u52a1\u961f\u5217\uff0c\u770b\u770b\u91cc\u9762\u8fd8\u6709\u54ea\u4e9b\u4e8b\u4ef6\uff0c\u90a3\u4e9b\u5bf9\u5e94\u7684\u5f02\u6b65\u4efb\u52a1\uff0c\u4e8e\u662f\u7ed3\u675f\u7b49\u5f85\u72b6\u6001\uff0c\u8fdb\u5165\u6267\u884c\u6808\uff0c\u5f00\u59cb\u6267\u884c<\/li>\n<li>\u4e3b\u7ebf\u7a0b\u4e0d\u65ad\u7684\u91cd\u590d\u4e0a\u9762\u7684\u7b2c\u4e09\u6b65<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/ycdledu-1301142972.cos.ap-chengdu.myqcloud.com\/2020\/10\/17\/16029190249224.jpg\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/ycdledu-1301142972.cos.ap-chengdu.myqcloud.com\/2020\/10\/17\/16029288993606.jpg\" alt=\"\" \/><\/p>\n<p>###JS \u5f02\u6b65\u7f16\u7a0b\u8fdb\u5316\u53f2<br \/>\n<img decoding=\"async\" src=\"https:\/\/ycdledu-1301142972.cos.ap-chengdu.myqcloud.com\/2020\/10\/17\/16029219677983.jpg\" alt=\"\" \/><\/p>\n<blockquote>\n<p>async\/await\u53ef\u4ee5\u8bf4\u662f\u5f02\u6b65\u7ec8\u6781\u89e3\u51b3\u65b9\u6848\u4e86<\/p>\n<\/blockquote>\n<p>##\u5f02\u6b65\u7f16\u7a0b<br \/>\n###\u56de\u8c03\u51fd\u6570(callback)<br \/>\nAJAX\u3002\u3002\u3002<br \/>\nsetTimeout<br \/>\n<img decoding=\"async\" src=\"https:\/\/ycdledu-1301142972.cos.ap-chengdu.myqcloud.com\/2020\/10\/17\/16029191225155.jpg\" alt=\"\" \/><\/p>\n<pre><code class=\"language-javascript\">setTimeout(() =&gt; {\n    task();\n},3000)\nconsole.log('\u6267\u884cconsole');\n\/\/ \u6267\u884cconsole \n\/\/ task()\n \u2043 \nsetTimeout(() =&gt; {\n    task()\n},3000)\nsleep(10000000)\n \u2043 \nconsole.log('\u5148\u6267\u884c\u8fd9\u91cc');\nsetTimeout(() =&gt; {\n    console.log('\u6267\u884c\u5566')\n},0);\n\/\/ \u5148\u6267\u884c\u8fd9\u91cc\n\/\/ \u6267\u884c\u5566\n<\/code><\/pre>\n<p>###\u56de\u8c03\u4e0d\u4e00\u5b9a\u662f\u5f02\u6b65<\/p>\n<pre><code class=\"language-javascript\">function A(callback){\n    console.log(&quot;A&quot;);\n    callback();  \/\/\u8c03\u7528\u8be5\u51fd\u6570\n}\nfunction B(){\n   console.log(&quot;B&quot;);\n}\nA(B); \n\n<\/code><\/pre>\n<blockquote><\/blockquote>\n<p>####\u56de\u8c03\u51fd\u6570\u7684\u4f18\u70b9\u662f\u7b80\u5355\u3001\u5bb9\u6613\u7406\u89e3\u548c\u90e8\u7f72<br \/>\n####\u7f3a\u70b9\u662f\u4e0d\u5229\u4e8e\u4ee3\u7801\u7684\u9605\u8bfb\u548c\u7ef4\u62a4\uff0c\u5404\u4e2a\u90e8\u5206\u4e4b\u95f4\u9ad8\u5ea6\u8026\u5408\uff08Coupling\uff09\uff0c\u6d41\u7a0b\u4f1a\u5f88\u6df7\u4e71\uff0c\u800c\u4e14\u6bcf\u4e2a\u4efb\u52a1\u53ea\u80fd\u6307\u5b9a\u4e00\u4e2a\u56de\u8c03\u51fd\u6570<\/p>\n<p>##Promise<\/p>\n<blockquote>\n<p>JavaScript\u5904\u7406\u5f02\u6b65\u90fd\u662f\u4ee5callback\u7684\u65b9\u5f0f\uff0c\u5728\u524d\u7aef\u5f00\u53d1\u9886\u57dfcallback\u673a\u5236\u51e0\u4e4e\u6df1\u5165\u4eba\u5fc3\uff0c\u8fd1\u51e0\u5e74\u968f\u7740JavaScript\u5f00\u53d1\u6a21\u5f0f\u7684\u9010\u6e10\u6210\u719f\uff0cCommonJS\u89c4\u8303\u987a\u52bf\u800c\u751f\uff0c\u5176\u4e2d\u5c31\u5305\u62ec\u63d0\u51fa\u4e86Promise\u89c4\u8303\uff0cPromise\u5b8c\u5168\u6539\u53d8\u4e86js\u5f02\u6b65\u7f16\u7a0b\u7684\u5199\u6cd5\uff0c\u8ba9\u5f02\u6b65\u7f16\u7a0b\u53d8\u5f97\u5341\u5206\u7684\u6613\u4e8e\u7406\u89e3\uff0c\u540c\u65f6Promise\u4e5f\u5df2\u7ecf\u7eb3\u5165\u4e86ES6\uff0c\u800c\u4e14\u9ad8\u7248\u672c\u7684chrome\u3001firefox\u6d4f\u89c8\u5668\u90fd\u5df2\u7ecf\u539f\u751f\u5b9e\u73b0\u4e86Promise<\/p>\n<\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/ycdledu-1301142972.cos.ap-chengdu.myqcloud.com\/2020\/10\/17\/16029216694310.jpg\" alt=\"\" \/><\/p>\n<table>\n<thead>\n<tr>\n<th>\u4f18\u70b9<\/th>\n<th>\u7f3a\u70b9<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u89e3\u51b3\u56de\u8c03<\/td>\n<td>\u65e0\u6cd5\u68c0\u67e5\u8fdb\u884c\u72b6\u6001<\/td>\n<\/tr>\n<tr>\n<td>\u94fe\u5f0f\u8c03\u7528<\/td>\n<td>\u5efa\u7acb\u4e14\u6267\u884c\u65e0\u6cd5\u53d6\u6d88<\/td>\n<\/tr>\n<tr>\n<td>\u51cf\u5c11\u5d4c\u5957<\/td>\n<td>\u5185\u90e8\u9519\u8bef\u65e0\u6cd5\u6355\u6349<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>\u4e00\u4e2apromise\u53ef\u80fd\u6709\u4e09\u79cd\u72b6\u6001\uff1a\u7b49\u5f85\uff08pending\uff09\u3001\u5df2\u5b8c\u6210\uff08fulfilled\uff09\u3001\u5df2\u62d2\u7edd\uff08rejected\uff09<\/li>\n<li>\u4e00\u4e2apromise\u7684\u72b6\u6001\u53ea\u53ef\u80fd\u4ece\u201c\u7b49\u5f85\u201d\u8f6c\u5230\u201c\u5b8c\u6210\u201d\u6001\u6216\u8005\u201c\u62d2\u7edd\u201d\u6001\uff0c\u4e0d\u80fd\u9006\u5411\u8f6c\u6362\uff0c\u540c\u65f6\u201c\u5b8c\u6210\u201d\u6001\u548c\u201c\u62d2\u7edd\u201d\u6001\u4e0d\u80fd\u76f8\u4e92\u8f6c\u6362<\/li>\n<li>promise\u5fc5\u987b\u5b9e\u73b0then\u65b9\u6cd5\uff08\u53ef\u4ee5\u8bf4\uff0cthen\u5c31\u662fpromise\u7684\u6838\u5fc3\uff09\uff0c\u800c\u4e14then\u5fc5\u987b\u8fd4\u56de\u4e00\u4e2apromise\uff0c\u540c\u4e00\u4e2apromise\u7684then\u53ef\u4ee5\u8c03\u7528\u591a\u6b21\uff0c\u5e76\u4e14\u56de\u8c03\u7684\u6267\u884c\u987a\u5e8f\u8ddf\u5b83\u4eec\u88ab\u5b9a\u4e49\u65f6\u7684\u987a\u5e8f\u4e00\u81f4<\/li>\n<li>then\u65b9\u6cd5\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6210\u529f\u65f6\u7684\u56de\u8c03\uff0c\u5728promise\u7531\u201c\u7b49\u5f85\u201d\u6001\u8f6c\u6362\u5230\u201c\u5b8c\u6210\u201d\u6001\u65f6\u8c03\u7528\uff0c\u53e6\u4e00\u4e2a\u662f\u5931\u8d25\u65f6\u7684\u56de\u8c03\uff0c\u5728promise\u7531\u201c\u7b49\u5f85\u201d\u6001\u8f6c\u6362\u5230\u201c\u62d2\u7edd\u201d\u6001\u65f6\u8c03\u7528\uff0c\u540c\u65f6\uff0cthen\u53ef\u4ee5\u63a5\u53d7\u53e6\u4e00\u4e2apromise\u4f20\u5165\uff0c\u4e5f\u63a5\u53d7\u4e00\u4e2a\u201c\u7c7bthen\u201d\u7684\u5bf9\u8c61\u6216\u65b9\u6cd5\uff0c\u5373thenable\u5bf9\u8c61<\/li>\n<\/ul>\n<p>new Promise(function (resolve, reject) {<br \/>\n\/\/ do &#8230;<br \/>\n});<\/p>\n<pre><code class=\"language-javascript\">if(typeof(Promise)===&quot;function&quot;) {\n    console.log(&quot;\u652f\u6301&quot;);\n}\nelse {\n    console.log(&quot;\u4e0d\u652f\u6301&quot;);\n}\n<\/code><\/pre>\n<pre><code class=\"language-javascript\">setTimeout(function () {\n    console.log(&quot;1&quot;);\n    setTimeout(function () {\n        console.log(&quot;2&quot;);\n        setTimeout(function () {\n            console.log(&quot;3&quot;);\n        }, 3000);\n    }, 4000);\n}, 1000);\n\n\n<\/code><\/pre>\n<pre><code class=\"language-javascript\">new Promise(function (resolve, reject) {\n    setTimeout(function () {\n        console.log(&quot;First&quot;);\n        resolve();\n    }, 1000);\n}).then(function () {\n    return new Promise(function (resolve, reject) {\n        setTimeout(function () {\n            console.log(&quot;Second&quot;);\n            resolve();\n        }, 4000);\n    });\n}).then(function () {\n    setTimeout(function () {\n        console.log(&quot;Third&quot;);\n    }, 3000);\n});\n<\/code><\/pre>\n<p>###\u683c\u5f0f<br \/>\nnew Promise(function (resolve, reject) {<br \/>\nconsole.log(&quot;Run&quot;);<br \/>\n});<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/ycdledu-1301142972.cos.ap-chengdu.myqcloud.com\/2020\/10\/17\/16029231400560.jpg\" alt=\"\" \/><\/p>\n<pre><code class=\"language-javascript\">new Promise(function (resolve, reject) {\n    console.log(1);\n    resolve(2);\n}).then(function (value) {\n    console.log(value);\n    return 3;\n}).then(function (value) {\n    console.log(value);\n    throw &quot;error&quot;;\n}).catch(function (err) {\n    console.log(err);\n});\n<\/code><\/pre>\n<p>###\u518d\u6765\u4e00\u79cd\u5199\u6cd5<\/p>\n<pre><code class=\"language-javascript\">let promise = new Promise(function(resolve, reject){\n    console.log(&quot;1&quot;);\n    resolve()\n});\npromise.then(() =&gt; console.log(&quot;2&quot;));\nconsole.log(&quot;3)\n<\/code><\/pre>\n<pre><code class=\"language-javascript\">console.log(&quot;program start....&quot;)\nfunction funs(c){\n return new Promise(function(resolve,reject){\n  resolve(c+&quot;,world&quot;);\n  \n });\n}\nfuns('hello')\n.then(function(retulet){\n console.log(retulet)\n})\n.then(function(){})\n.catch(function(){})\nconsole.log(&quot;program end....&quot;)\n\n\n<\/code><\/pre>\n<p>###promise copy<\/p>\n<pre><code class=\"language-javascript\">var hello = new Promise(function(resolve,reject){\n    setTimeout(function(){\n     var text = &quot;\u4f60\u597d\uff0c\u6211\u662fsaoge&quot;;\n     console.log(text);\n     resolve(text);\n    },1000)\n   })\n   var say = new Promise(function(resolve,reject){\n    setTimeout(function(text){\n     console.log(text);\n    },1000)\n   })\n<\/code><\/pre>\n<p>##\u6df7\u5408\u5f02\u6b65<\/p>\n<pre><code class=\"language-javascript\">setTimeout(function() {\n    console.log('\u6211\u662f\u5b9a\u65f6\u5668\uff01');\n})\nnew Promise(function(resolve) {\n    console.log('\u6211\u662fpromise\uff01');\n    resolve();\n}).then(function() {\n    console.log('\u6211\u662fthen\uff01');\n})\nconsole.log('\u6211\u662f\u4e3b\u7ebf\u7a0b\uff01');\n<\/code><\/pre>\n<hr \/>\n<p>###\u9898\u76ee\u4e00\uff1a<br \/>\nsetTimeout(() =&gt; {<br \/>\nconsole.log(1);<br \/>\n}, 0);<\/p>\n<p>new Promise((resolve) =&gt; {<br \/>\nconsole.log(2);<br \/>\nresolve();<br \/>\n}).then(() =&gt; {<br \/>\nconsole.log(3);<br \/>\n});<\/p>\n<p>console.log(4);<br \/>\n\/\/ \u8f93\u51fa\u6700\u540e\u7684\u7ed3\u679c<\/p>\n<hr \/>\n<blockquote>\n<p>\uff081\uff09setTimeout\u4e22\u7ed9\u6d4f\u89c8\u5668\u7684\u5f02\u6b65\u7ebf\u7a0b\u5904\u7406\uff0c\u56e0\u4e3a\u65f6\u95f4\u662f0\uff0c\u9a6c\u4e0a\u653e\u5165\u6d88\u606f\u961f\u5217<br \/>\n\uff082\uff09new Promise\u91cc\u9762\u7684console.log(2)\u52a0\u5165\u6267\u884c\u6808\uff0c\u5e76\u6267\u884c\uff0c\u7136\u540e\u9000\u51fa<br \/>\n\uff083\uff09\u76f4\u63a5resolve\uff0cthen\u540e\u9762\u7684\u5185\u5bb9\u52a0\u5165\u5fae\u4efb\u52a1\u961f\u5217<br \/>\n\uff084\uff09console.log(4)\u52a0\u5165\u6267\u884c\u6808\uff0c\u6267\u884c\u5b8c\u6210\u540e\u9000\u51fa<br \/>\n\uff085\uff09\u68c0\u67e5\u5fae\u4efb\u52a1\u961f\u5217\uff0c\u53d1\u73b0\u6709\u4efb\u52a1\uff0c\u6267\u884cconsole.log(3)<br \/>\n\uff086\uff09\u53d1\u73b0\u6d88\u606f\u961f\u5217\u6709\u4efb\u52a1\uff0c\u6267\u884c\u4e0b\u4e00\u6b21\u5b8f\u4efb\u52a1console.log(1)<\/p>\n<\/blockquote>\n<p>###\u9898\u76ee\u4e8c\uff1a<br \/>\nsetTimeout(() =&gt; {<br \/>\nconsole.log(1);<br \/>\n}, 0);<\/p>\n<p>new Promise((resolve) =&gt; {<br \/>\nconsole.log(2);<br \/>\nsetTimeout(() =&gt; {<br \/>\nconsole.log(5);<br \/>\n}, 0);<br \/>\nresolve();<br \/>\n}).then(() =&gt; {<br \/>\nconsole.log(3);<br \/>\n});<\/p>\n<h2><a id=\"console-log-4%E8%BE%93%E5%87%BA%E6%9C%80%E5%90%8E%E7%9A%84%E7%BB%93%E6%9E%9C\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>console.log(4);<br \/>\n\/\/ \u8f93\u51fa\u6700\u540e\u7684\u7ed3\u679c<\/h2>\n<blockquote>\n<p>\uff081\uff09setTimeout\u4e22\u7ed9\u6d4f\u89c8\u5668\u7684\u5f02\u6b65\u7ebf\u7a0b\u5904\u7406\uff0c\u56e0\u4e3a\u65f6\u95f4\u662f0\uff0c\u9a6c\u4e0a\u653e\u5165\u6d88\u606f\u961f\u5217<br \/>\n\uff082\uff09new Promise\u91cc\u9762\u7684console.log(2)\u52a0\u5165\u6267\u884c\u6808\uff0c\u5e76\u6267\u884c<br \/>\n\uff083\uff09setTimeout\u7ed9\u6d4f\u89c8\u5668\u7684\u5f02\u6b65\u7ebf\u7a0b\u5904\u7406\uff0c\u56e0\u4e3a\u65f6\u95f4\u662f0\uff0c\u9a6c\u4e0a\u653e\u5165\u6d88\u606f\u961f\u5217\uff0c\u7136\u540e\u9000\u51fa<br \/>\n\uff084\uff09\u76f4\u63a5resolve\uff0cthen\u540e\u9762\u7684\u5185\u5bb9\u52a0\u5165\u5fae\u4efb\u52a1\u961f\u5217<br \/>\n\uff085\uff09console.log(4)\u52a0\u5165\u6267\u884c\u6808\uff0c\u6267\u884c\u5b8c\u6210\u540e\u9000\u51fa<br \/>\n\uff086\uff09\u68c0\u67e5\u5fae\u4efb\u52a1\u961f\u5217\uff0c\u53d1\u73b0\u6709\u4efb\u52a1\uff0c\u6267\u884cconsole.log(3)<br \/>\n\uff087\uff09\u53d1\u73b0\u6d88\u606f\u961f\u5217\u6709\u4efb\u52a1\uff0c\u6267\u884c\u4e0b\u4e00\u6b21\u5b8f\u4efb\u52a1console.log(1)<br \/>\n\uff088\uff09\u53d1\u73b0\u6d88\u606f\u961f\u5217\u6709\u4efb\u52a1\uff0c\u6267\u884c\u4e0b\u4e00\u6b21\u5b8f\u4efb\u52a1console.log(5)<\/p>\n<\/blockquote>\n<h2><a id=\"%E9%A2%98%E7%9B%AE%E4%B8%89%EF%BC%9Asettimeout-console-log-1-0-new-promise-resolve-reject-console-log-2-resolve-3-then-val-console-log-val-console-log-4%E8%BE%93%E5%87%BA%E6%9C%80%E5%90%8E%E7%9A%84%E7%BB%93%E6%9E%9C\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>###\u9898\u76ee\u4e09\uff1a<br \/>\nsetTimeout(() =&gt; {<br \/>\nconsole.log(1);<br \/>\n}, 0);<br \/>\nnew Promise((resolve,reject) =&gt;{<br \/>\nconsole.log(2)<br \/>\nresolve(3)<br \/>\n}).then((val) =&gt;{<br \/>\nconsole.log(val);<br \/>\n})<br \/>\nconsole.log(4);<br \/>\n\/\/ \u8f93\u51fa\u6700\u540e\u7684\u7ed3\u679c<\/h2>\n<blockquote>\n<p>1\uff09\u5148\u6267\u884cscript\u540c\u6b65\u4ee3\u7801\uff1a<br \/>\n\u5148\u6267\u884cnew Promise\u4e2d\u7684console.log(2),then\u540e\u9762\u7684\u4e0d\u6267\u884c\u5c5e\u4e8e\u5fae\u4efb\u52a1\u7136\u540e\u6267\u884cconsole.log(4)<br \/>\n\uff082\uff09\u6267\u884c\u5b8cscript\u5b8f\u4efb\u52a1\u540e\uff0c\u6267\u884c\u5fae\u4efb\u52a1\uff0cconsole.log(3)\uff0c\u6ca1\u6709\u5176\u4ed6\u5fae\u4efb\u52a1\u4e86<br \/>\n\uff083\uff09\u6267\u884c\u53e6\u4e00\u4e2a\u5b8f\u4efb\u52a1\uff0c\u5b9a\u65f6\u5668\uff0cconsole.log(1)<\/p>\n<\/blockquote>\n<p>####\u9898\u76ee\u56db\uff1a<br \/>\nlet a = () =&gt; {<br \/>\nsetTimeout(() =&gt; {<br \/>\nconsole.log(&#8216;\u4efb\u52a1\u961f\u5217\u51fd\u65701&#8217;)<br \/>\n}, 0)<br \/>\nfor (let i = 0; i &lt; 5000; i++) {<br \/>\nconsole.log(&#8216;a\u7684for\u5faa\u73af&#8217;)<br \/>\n}<br \/>\nconsole.log(&#8216;a\u4e8b\u4ef6\u6267\u884c\u5b8c&#8217;)<br \/>\n}<\/p>\n<p>let b = () =&gt; {<br \/>\nsetTimeout(() =&gt; {<br \/>\nconsole.log(&#8216;\u4efb\u52a1\u961f\u5217\u51fd\u65702&#8217;)<br \/>\n}, 0)<br \/>\nfor (let i = 0; i &lt; 5000; i++) {<br \/>\nconsole.log(&#8216;b\u7684for\u5faa\u73af&#8217;)<br \/>\n}<br \/>\nconsole.log(&#8216;b\u4e8b\u4ef6\u6267\u884c\u5b8c&#8217;)<br \/>\n}<\/p>\n<p>let c = () =&gt; {<br \/>\nsetTimeout(() =&gt; {<br \/>\nconsole.log(&#8216;\u4efb\u52a1\u961f\u5217\u51fd\u65703&#8217;)<br \/>\n}, 0)<br \/>\nfor (let i = 0; i &lt; 5000; i++) {<br \/>\nconsole.log(&#8216;c\u7684for\u5faa\u73af&#8217;)<br \/>\n}<br \/>\nconsole.log(&#8216;c\u4e8b\u4ef6\u6267\u884c\u5b8c&#8217;)<br \/>\n}<\/p>\n<p>a();<br \/>\nb();<br \/>\nc();<br \/>\n\/\/ \u8f93\u51fa\u6700\u540e\u7684\u7ed3\u679c<\/p>\n<blockquote>\n<p>\u7ed3\u679c\u662f\u5f53a\u3001b\u3001c\u51fd\u6570\u90fd\u6267\u884c\u5b8c\u6210\u4e4b\u540e\uff0c\u4e09\u4e2asetTimeout\u624d\u4f1a\u4f9d\u6b21\u6267\u884c<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>##JS\u4e09\u5ea7\u5927\u5c71 JavaScript\u662f\u4e00\u95e8\u5355\u7ebf\u7a0b\u7684\u8bed\u8a00\uff0c\u56e0\u6b64\uff0cJavaScript\u5728\u540c\u4e00\u4e2a\u65f6\u95f4\u53ea\u80fd\u505a\u4e00\u4ef6\u4e8b\uff0c [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[103],"tags":[108],"class_list":["post-7218","post","type-post","status-publish","format-standard","hentry","category-qianduan","tag-108"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=7218"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7218\/revisions"}],"predecessor-version":[{"id":7219,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7218\/revisions\/7219"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=7218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=7218"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=7218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}