{"id":33275,"date":"2024-11-25T13:05:57","date_gmt":"2024-11-25T05:05:57","guid":{"rendered":"https:\/\/fwq.ai\/blog\/33275\/"},"modified":"2024-11-25T13:05:57","modified_gmt":"2024-11-25T05:05:57","slug":"ecmascript-2020-%e7%9a%84%e6%96%b0%e7%89%b9%e6%80%a7","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/33275\/","title":{"rendered":"ECMAScript 2020 \u7684\u65b0\u7279\u6027"},"content":{"rendered":"<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/image\/253\/885\/766\/1591930133483773.png\" class=\"aligncenter\" title=\"ECMAScript 2020 \u7684\u65b0\u7279\u6027\u63d2\u56fe\" alt=\"ECMAScript 2020 \u7684\u65b0\u7279\u6027\u63d2\u56fe\" \/><\/p>\n<p>javascript \u662f\u6700\u6d41\u884c\u7684\u7f16\u7a0b\u8bed\u8a00\u4e4b\u4e00\uff0c\u6bcf\u5e74\u90fd\u4f1a\u6dfb\u52a0\u65b0\u7684\u7279\u6027\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u6dfb\u52a0\u5728 ecmascript 2020\uff08\u53c8\u79f0es11\uff09\u4e2d\u7684\u65b0\u7279\u6027\u3002<\/p>\n<p>\u5728\u5f15\u5165 ECMAScript 2015\uff08\u53c8\u79f0 ES6\uff09\u4e4b\u524d\uff0cJavaScript \u53d1\u5c55\u7684\u975e\u5e38\u7f13\u6162\u3002\u4f46\u81ea 2015 \u5e74\u8d77\uff0c\u6bcf\u5e74\u90fd\u6709\u65b0\u7279\u6027\u6dfb\u52a0\u8fdb\u6765\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u662f\u6240\u6709\u7279\u6027\u90fd\u88ab\u73b0\u4ee3\u6d4f\u89c8\u5668\u652f\u6301\uff0c\u4f46\u662f\u7531\u4e8e JavaScript \u7f16\u8bd1\u5668 Babel \u7684\u5b58\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u53ef\u4ee5\u4f7f\u7528\u65b0\u7279\u6027\u4e86\u3002\u672c\u6587\u5c06\u4ecb\u7ecd ECMAScript 2020\uff08ES11\uff09\u7684\u4e00\u4e9b\u6700\u65b0\u7279\u6027\u3002<\/p>\n<p><strong>Optional Chaining \u53ef\u9009\u94fe\u5f0f\u8c03\u7528<\/strong><\/p>\n<hr>\n<p>\u5927\u90e8\u5206\u5f00\u53d1\u8005\u90fd\u9047\u5230\u8fc7\u8fd9\u4e2a\u95ee\u9898\uff1a<\/p>\n<p>TypeError: Cannot read property \u2018x\u2019 of undefined<\/p>\n<p>\u8fd9\u4e2a\u9519\u8bef\u8868\u793a\u6211\u4eec\u6b63\u5728\u8bbf\u95ee\u4e00\u4e2a\u4e0d\u5c5e\u4e8e\u5bf9\u8c61\u7684\u5c5e\u6027\u3002<\/p>\n<p style=\"line-height: normal\"><strong>\u8bbf\u95ee\u5bf9\u8c61\u7684\u5c5e\u6027<\/strong><\/p>\n<pre>const&nbsp;flower&nbsp;=&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;colors:&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;red:&nbsp;true\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}\nconsole.log(flower.colors.red)&nbsp;\/\/&nbsp;\u6b63\u5e38\u8fd0\u884c\n\nconsole.log(flower.species.lily)&nbsp;\/\/&nbsp;\u629b\u51fa\u9519\u8bef\uff1aTypeError:&nbsp;Cannot&nbsp;read&nbsp;property&nbsp;'lily'&nbsp;of&nbsp;undefined<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cJavaScript \u5f15\u64ce\u4f1a\u50cf\u8fd9\u6837\u629b\u51fa\u9519\u8bef\u3002\u4f46\u662f\u67d0\u4e9b\u60c5\u51b5\u4e0b\u503c\u662f\u5426\u5b58\u5728\u5e76\u4e0d\u91cd\u8981\uff0c\u56e0\u4e3a\u6211\u4eec\u77e5\u9053\u5b83\u4f1a\u5b58\u5728\u3002\u4e8e\u662f\uff0c\u53ef\u9009\u94fe\u5f0f\u8c03\u7528\u5c31\u6d3e\u4e0a\u7528\u573a\u4e86\uff01<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7531\u4e00\u4e2a\u95ee\u53f7\u548c\u4e00\u4e2a\u70b9\u7ec4\u6210\u7684\u53ef\u9009\u94fe\u5f0f\u64cd\u4f5c\u7b26\uff0c\u53bb\u8868\u793a\u4e0d\u5e94\u8be5\u5f15\u53d1\u9519\u8bef\u3002\u5982\u679c\u6ca1\u6709\u503c\uff0c\u5e94\u8be5\u8fd4\u56de undefined\u3002<\/p>\n<pre>console.log(flower.species?.lily)&nbsp;\/\/&nbsp;\u8f93\u51fa&nbsp;undefined<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5f53\u8bbf\u95ee\u6570\u7ec4\u6216\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u53ef\u9009\u94fe\u5f0f\u8c03\u7528\u3002<\/p>\n<p><strong>\u8bbf\u95ee\u6570\u7ec4<\/strong><\/p>\n<pre>let&nbsp;flowers&nbsp;=&nbsp;&nbsp;['lily',&nbsp;'daisy',&nbsp;'rose']\n\nconsole.log(flowers[1])&nbsp;\/\/&nbsp;\u8f93\u51fa\uff1adaisy\n\nflowers&nbsp;=&nbsp;null\n\nconsole.log(flowers[1])&nbsp;\/\/&nbsp;\u629b\u51fa\u9519\u8bef\uff1aTypeError:&nbsp;Cannot&nbsp;read&nbsp;property&nbsp;'1'&nbsp;of&nbsp;null\nconsole.log(flowers?.[1])&nbsp;\/\/&nbsp;\u8f93\u51fa\uff1aundefined<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8c03\u7528\u51fd\u6570<\/p>\n<pre>let&nbsp;plantFlowers&nbsp;=&nbsp;()&nbsp;=&amp;gt;&nbsp;{\n&nbsp;&nbsp;return&nbsp;'orchids'\n}\n\nconsole.log(plantFlowers())&nbsp;\/\/&nbsp;\u8f93\u51fa\uff1aorchids\n\nplantFlowers&nbsp;=&nbsp;null\n\nconsole.log(plantFlowers())&nbsp;\/\/&nbsp;\u629b\u51fa\u9519\u8bef\uff1aTypeError:&nbsp;plantFlowers&nbsp;is&nbsp;not&nbsp;a&nbsp;function\n\nconsole.log(plantFlowers?.())&nbsp;\/\/&nbsp;\u8f93\u51fa\uff1aundefined<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>Nullish Coalescing \u7a7a\u503c\u5408\u5e76<\/strong><\/p>\n<hr>\n<p>\u76ee\u524d\uff0c\u8981\u4e3a\u53d8\u91cf\u63d0\u4f9b\u56de\u9000\u503c\uff0c\u903b\u8f91\u64cd\u4f5c\u7b26 || \u8fd8\u662f\u5fc5\u987b\u7684\u3002\u5b83\u9002\u7528\u4e8e\u5f88\u591a\u60c5\u51b5\uff0c\u4f46\u4e0d\u80fd\u5e94\u7528\u5728\u4e00\u4e9b\u7279\u6b8a\u7684\u573a\u666f\u3002\u4f8b\u5982\uff0c\u521d\u59cb\u503c\u662f\u5e03\u5c14\u503c\u6216\u6570\u5b57\u7684\u60c5\u51b5\u3002\u4e3e\u4f8b\u8bf4\u660e\uff0c\u6211\u4eec\u8981\u628a\u6570\u5b57\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u5f53\u53d8\u91cf\u7684\u521d\u59cb\u503c\u4e0d\u662f\u6570\u5b57\u65f6\uff0c\u5c31\u9ed8\u8ba4\u5176\u4e3a 7 \uff1a<\/p>\n<pre>let&nbsp;number&nbsp;=&nbsp;1\nlet&nbsp;myNumber&nbsp;=&nbsp;number&nbsp;||&nbsp;7<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u53d8\u91cf myNumber \u7b49\u4e8e 1\uff0c\u56e0\u4e3a\u5de6\u8fb9\u7684\uff08number\uff09\u662f\u4e00\u4e2a \u771f \u503c 1\u3002\u4f46\u662f\uff0c\u5f53\u53d8\u91cf number \u4e0d\u662f 1 \u800c\u662f 0 \u5462\uff1f<\/p>\n<pre>let&nbsp;number&nbsp;=&nbsp;0\nlet&nbsp;myNumber&nbsp;=&nbsp;number&nbsp;||&nbsp;7<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>0 \u662f&nbsp;\u5047&nbsp;\u503c\uff0c\u6240\u4ee5\u5373\u4f7f 0 \u662f\u6570\u5b57\u3002\u53d8\u91cf&nbsp;myNumber&nbsp;\u5c06\u4f1a\u88ab\u8d4b\u503c\u4e3a\u53f3\u8fb9\u7684 7\u3002\u4f46\u7ed3\u679c\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u3002\u5e78\u597d\uff0c\u7531\u4e24\u4e2a\u95ee\u53f7\u7ec4\u6210\uff1a??&nbsp;\u7684\u5408\u5e76\u64cd\u4f5c\u7b26\u5c31\u53ef\u4ee5\u68c0\u67e5\u53d8\u91cf&nbsp;number&nbsp;\u662f\u5426\u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u800c\u4e0d\u7528\u5199\u989d\u5916\u7684\u4ee3\u7801\u4e86\u3002<\/p>\n<pre>let&nbsp;number&nbsp;=&nbsp;0\nlet&nbsp;myNumber&nbsp;=&nbsp;number&nbsp;??&nbsp;7<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u64cd\u4f5c\u7b26\u53f3\u8fb9\u7684\u503c\u4ec5\u5728\u5de6\u8fb9\u7684\u503c\u7b49\u4e8e null \u6216 undefined \u65f6\u6709\u6548\uff0c\u56e0\u6b64\uff0c\u4f8b\u5b50\u4e2d\u7684\u53d8\u91cf myNumber \u73b0\u5728\u7684\u503c\u7b49\u4e8e 0 \u4e86\u3002<\/p>\n<p><strong>Private Fields \u79c1\u6709\u5b57\u6bb5<\/strong><\/p>\n<hr>\n<p>\u8bb8\u591a\u5177\u6709 classes \u7684\u7f16\u7a0b\u8bed\u8a00\u5141\u8bb8\u5b9a\u4e49\u7c7b\u4f5c\u4e3a\u516c\u5171\u7684\uff0c\u53d7\u4fdd\u62a4\u7684\u6216\u79c1\u6709\u7684\u5c5e\u6027\u3002Public \u5c5e\u6027\u53ef\u4ee5\u4ece\u7c7b\u7684\u5916\u90e8\u6216\u8005\u5b50\u7c7b\u8bbf\u95ee\uff0cprotected \u5c5e\u6027\u53ea\u80fd\u88ab\u5b50\u7c7b\u8bbf\u95ee\uff0cprivate \u5c5e\u6027\u53ea\u80fd\u88ab\u7c7b\u5185\u90e8\u8bbf\u95ee\u3002JavaScript \u4ece ES6 \u5f00\u59cb\u652f\u6301\u7c7b\u8bed\u6cd5\uff0c\u4f46\u76f4\u5230\u73b0\u5728\u624d\u5f15\u5165\u4e86\u79c1\u6709\u5b57\u6bb5\u3002\u8981\u5b9a\u4e49\u79c1\u6709\u5c5e\u6027\uff0c\u5fc5\u987b\u5728\u5176\u524d\u9762\u52a0\u4e0a\u6563\u5217\u7b26\u53f7\uff1a#\u3002<\/p>\n<pre>class&nbsp;Flower&nbsp;{\n&nbsp;&nbsp;#leaf_color&nbsp;=&nbsp;\"green\";\n&nbsp;&nbsp;constructor(name)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;this.name&nbsp;=&nbsp;name;\n&nbsp;&nbsp;}\n\n&nbsp;&nbsp;get_color()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;this.#leaf_color;\n&nbsp;&nbsp;}\n}\n\nconst&nbsp;orchid&nbsp;=&nbsp;new&nbsp;Flower(\"orchid\");\n\nconsole.log(orchid.get_color());&nbsp;\/\/&nbsp;\u8f93\u51fa\uff1agreen\nconsole.log(orchid.#leaf_color)&nbsp;\/\/&nbsp;\u62a5\u9519\uff1aSyntaxError:&nbsp;Private&nbsp;field&nbsp;'#leaf_color'&nbsp;must&nbsp;be&nbsp;declared&nbsp;in&nbsp;an&nbsp;enclosing&nbsp;class<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u679c\u6211\u4eec\u4ece\u5916\u90e8\u8bbf\u95ee\u7c7b\u7684\u79c1\u6709\u5c5e\u6027\uff0c\u52bf\u5fc5\u4f1a\u62a5\u9519\u3002<\/p>\n<p><strong>Static Fields \u9759\u6001\u5b57\u6bb5<\/strong><\/p>\n<hr>\n<p>\u5982\u679c\u60f3\u4f7f\u7528\u7c7b\u7684\u65b9\u6cd5\uff0c\u9996\u5148\u5fc5\u987b\u5b9e\u4f8b\u5316\u4e00\u4e2a\u7c7b\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>class&nbsp;Flower&nbsp;{\n&nbsp;&nbsp;add_leaves()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;console.log(\"Adding&nbsp;leaves\");\n&nbsp;&nbsp;}\n}\n\nconst&nbsp;rose&nbsp;=&nbsp;new&nbsp;Flower();\nrose.add_leaves();\n\nFlower.add_leaves()&nbsp;\/\/&nbsp;\u629b\u51fa\u9519\u8bef\uff1aTypeError:&nbsp;Flower.add_leaves&nbsp;is&nbsp;not&nbsp;a&nbsp;function<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8bd5\u56fe\u53bb\u8bbf\u95ee\u6ca1\u6709\u5b9e\u4f8b\u5316\u7684&nbsp;Flower&nbsp;\u7c7b\u7684\u65b9\u6cd5\u5c06\u4f1a\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\u3002\u4f46\u7531\u4e8e&nbsp;static&nbsp;\u5b57\u6bb5\uff0c\u7c7b\u65b9\u6cd5\u53ef\u4ee5\u88ab&nbsp;static&nbsp;\u5173\u952e\u8bcd\u58f0\u660e\u7136\u540e\u4ece\u5916\u90e8\u8c03\u7528\u3002<\/p>\n<pre>class&nbsp;Flower&nbsp;{\n&nbsp;&nbsp;constructor(type)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;this.type&nbsp;=&nbsp;type;\n&nbsp;&nbsp;}\n&nbsp;&nbsp;static&nbsp;create_flower(type)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;new&nbsp;Flower(type);\n&nbsp;&nbsp;}\n}\n\nconst&nbsp;rose&nbsp;=&nbsp;Flower.create_flower(\"rose\");&nbsp;\/\/&nbsp;\u6b63\u5e38\u8fd0\u884c<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>Top Level Await \u9876\u7ea7 Await<\/strong><\/p>\n<hr>\n<p>\u76ee\u524d\uff0c\u5982\u679c\u7528&nbsp;await&nbsp;\u83b7\u53d6 promise \u51fd\u6570\u7684\u7ed3\u679c\uff0c\u90a3\u4f7f\u7528&nbsp;await&nbsp;\u7684\u51fd\u6570\u5fc5\u987b\u7528&nbsp;async&nbsp;\u5173\u952e\u5b57\u5b9a\u4e49\u3002<\/p>\n<pre>const&nbsp;func&nbsp;=&nbsp;async&nbsp;()&nbsp;=&amp;gt;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;response&nbsp;=&nbsp;await&nbsp;fetch(url)\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5934\u75bc\u7684\u662f\uff0c\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u53bb\u7b49\u5f85\u67d0\u4e9b\u7ed3\u679c\u57fa\u672c\u4e0a\u662f\u4e0d\u53ef\u80fd\u7684\u3002\u9664\u975e\u4f7f\u7528&nbsp;\u7acb\u5373\u8c03\u7528\u7684\u51fd\u6570\u8868\u8fbe\u5f0f\uff08IIFE\uff09\u3002<\/p>\n<pre>(async&nbsp;()&nbsp;=&amp;gt;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;response&nbsp;=&nbsp;await&nbsp;fetch(url)\n})()<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f46\u5f15\u5165\u4e86&nbsp;\u9876\u7ea7 Await&nbsp;\u540e\uff0c\u4e0d\u9700\u8981\u518d\u628a\u4ee3\u7801\u5305\u88f9\u5728\u4e00\u4e2a async \u51fd\u6570\u4e2d\u4e86\uff0c\u5982\u4e0b\u5373\u53ef\uff1a<\/p>\n<pre>const&nbsp;response&nbsp;=&nbsp;await&nbsp;fetch(url)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u4e2a\u7279\u6027\u5bf9\u4e8e\u89e3\u51b3\u6a21\u5757\u4f9d\u8d56\u6216\u5f53\u521d\u59cb\u6e90\u65e0\u6cd5\u4f7f\u7528\u800c\u9700\u8981\u5907\u7528\u6e90\u7684\u65f6\u5019\u662f\u975e\u5e38\u6709\u7528\u7684\u3002<\/p>\n<pre>let&nbsp;Vue\ntry&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;Vue&nbsp;=&nbsp;await&nbsp;import('url_1_to_vue')\n}&nbsp;catch&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;Vue&nbsp;=&nbsp;await&nbsp;import('url_2_to_vue)\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>Promise.allSettled \u65b9\u6cd5<\/p>\n<p>\u7b49\u5f85\u591a\u4e2a promise \u8fd4\u56de\u7ed3\u679c\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u7528 Promise.all([promise_1, promise_2])\u3002\u4f46\u95ee\u9898\u662f\uff0c\u5982\u679c\u5176\u4e2d\u4e00\u4e2a\u8bf7\u6c42\u5931\u8d25\u4e86\uff0c\u5c31\u4f1a\u629b\u51fa\u9519\u8bef\u3002\u7136\u800c\uff0c\u6709\u65f6\u5019\u6211\u4eec\u5e0c\u671b\u67d0\u4e2a\u8bf7\u6c42\u5931\u8d25\u540e\uff0c\u5176\u4ed6\u8bf7\u6c42\u7684\u7ed3\u679c\u80fd\u591f\u6b63\u5e38\u8fd4\u56de\u3002\u9488\u5bf9\u8fd9\u79cd\u60c5\u51b5 ES11 \u5f15\u5165\u4e86 Promise.allSettled \u3002<\/p>\n<pre>promise_1&nbsp;=&nbsp;Promise.resolve('hello')\npromise_2&nbsp;=&nbsp;new&nbsp;Promise((resolve,&nbsp;reject)&nbsp;=&amp;gt;&nbsp;setTimeout(reject,&nbsp;200,&nbsp;'problem'))\n\nPromise.allSettled([promise_1,&nbsp;promise_2])\n&nbsp;&nbsp;&nbsp;&nbsp;.then(([promise_1_result,&nbsp;promise_2_result])&nbsp;=&amp;gt;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.log(promise_1_result)&nbsp;\/\/&nbsp;\u8f93\u51fa\uff1a{status:&nbsp;'fulfilled',&nbsp;value:&nbsp;'hello'}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.log(promise_2_result)&nbsp;\/\/&nbsp;\u8f93\u51fa\uff1a{status:&nbsp;'rejected',&nbsp;reason:&nbsp;'problem'}\n&nbsp;&nbsp;&nbsp;&nbsp;})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6210\u529f\u7684 promise \u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b status \u548c value \u7684\u5bf9\u8c61\uff0c\u5931\u8d25\u7684 promise \u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b status \u548c reason \u7684\u5bf9\u8c61\u3002<\/p>\n<p><strong>Dynamic Import \u52a8\u6001\u5f15\u5165<\/strong><\/p>\n<hr>\n<p>\u4f60\u4e5f\u8bb8\u5728 webpack \u7684\u6a21\u5757\u7ed1\u5b9a\u4e2d\u5df2\u7ecf\u4f7f\u7528\u8fc7\u52a8\u6001\u5f15\u5165\u3002\u4f46\u5bf9\u4e8e\u8be5\u7279\u6027\u7684\u539f\u751f\u652f\u6301\u5df2\u7ecf\u5230\u6765\uff1a<\/p>\n<pre>\/\/&nbsp;Alert.js\nexport&nbsp;default&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;show()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u4ee3\u7801\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}\n\n\n\/\/&nbsp;\u4f7f\u7528&nbsp;Alert.js&nbsp;\u7684\u6587\u4ef6\nimport('\/components\/Alert.js')\n&nbsp;&nbsp;&nbsp;&nbsp;.then(Alert&nbsp;=&amp;gt;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Alert.show()\n&nbsp;&nbsp;&nbsp;&nbsp;})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8003\u8651\u5230\u8bb8\u591a\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u8bf8\u5982 webpack \u4e4b\u7c7b\u7684\u6a21\u5757\u6253\u5305\u5668\u6765\u8fdb\u884c\u4ee3\u7801\u7684\u8f6c\u8bd1\u548c\u4f18\u5316\uff0c\u8fd9\u4e2a\u7279\u6027\u73b0\u5728\u8fd8\u6ca1\u4ec0\u4e48\u5927\u4f5c\u7528\u3002<\/p>\n<p><strong>MatchAll \u5339\u914d\u6240\u6709\u9879<\/strong><\/p>\n<hr>\n<p>\u5982\u679c\u4f60\u60f3\u8981\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u9879\u548c\u5b83\u4eec\u7684\u4f4d\u7f6e\uff0cMatchAll \u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre>const&nbsp;regex&nbsp;=&nbsp;\/(apple)+\/;\nconst&nbsp;fruits&nbsp;=&nbsp;\"pear,&nbsp;apple,&nbsp;banana,&nbsp;apple,&nbsp;orange,&nbsp;apple\";\n\n\nfor&nbsp;(const&nbsp;match&nbsp;of&nbsp;fruits.match(regex))&nbsp;{\n&nbsp;&nbsp;console.log(match);&nbsp;\n}\n\/\/&nbsp;\u8f93\u51fa&nbsp;\n\/\/&nbsp;\n\/\/&nbsp;'apple'&nbsp;\n\/\/&nbsp;'apple'<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u76f8\u6bd4\u4e4b\u4e0b\uff0cmatchAll&nbsp;\u8fd4\u56de\u66f4\u591a\u7684\u4fe1\u606f\uff0c\u5305\u62ec\u627e\u5230\u5339\u914d\u9879\u7684\u7d22\u5f15\u3002<\/p>\n<pre>for&nbsp;(const&nbsp;match&nbsp;of&nbsp;fruits.matchAll(regex))&nbsp;{\n&nbsp;&nbsp;console.log(match);\n}\n\n\/\/&nbsp;\u8f93\u51fa\n\/\/&nbsp;\n\/\/&nbsp;[\n\/\/&nbsp;&nbsp;&nbsp;'apple',\n\/\/&nbsp;&nbsp;&nbsp;'apple',\n\/\/&nbsp;&nbsp;&nbsp;index:&nbsp;6,\n\/\/&nbsp;&nbsp;&nbsp;input:&nbsp;'pear,&nbsp;apple,&nbsp;banana,&nbsp;apple,&nbsp;orange,&nbsp;apple',\n\/\/&nbsp;&nbsp;&nbsp;groups:&nbsp;undefined\n\/\/&nbsp;],\n\/\/&nbsp;[\n\/\/&nbsp;&nbsp;&nbsp;'apple',\n\/\/&nbsp;&nbsp;&nbsp;'apple',\n\/\/&nbsp;&nbsp;&nbsp;index:&nbsp;21,\n\/\/&nbsp;&nbsp;&nbsp;input:&nbsp;'pear,&nbsp;apple,&nbsp;banana,&nbsp;apple,&nbsp;orange,&nbsp;apple',\n\/\/&nbsp;&nbsp;&nbsp;groups:&nbsp;undefined\n\/\/&nbsp;],\n\/\/&nbsp;[\n\/\/&nbsp;&nbsp;&nbsp;'apple',\n\/\/&nbsp;&nbsp;&nbsp;'apple',\n\/\/&nbsp;&nbsp;&nbsp;index:&nbsp;36,\n\/\/&nbsp;&nbsp;&nbsp;input:&nbsp;'pear,&nbsp;apple,&nbsp;banana,&nbsp;apple,&nbsp;orange,&nbsp;apple',\n\/\/&nbsp;&nbsp;&nbsp;groups:&nbsp;undefined\n\/\/&nbsp;]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>globalThis \u5168\u5c40\u5bf9\u8c61<\/strong><\/p>\n<hr>\n<p>JavaScript \u53ef\u4ee5\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u8fd0\u884c\uff0c\u6bd4\u5982\u6d4f\u89c8\u5668\u6216\u8005 Node.js\u3002\u6d4f\u89c8\u5668\u4e2d\u53ef\u7528\u7684\u5168\u5c40\u5bf9\u8c61\u662f\u53d8\u91cf window\uff0c\u4f46\u5728 Node.js \u4e2d\u662f\u4e00\u4e2a\u53eb\u505a global \u7684\u5bf9\u8c61\u3002\u4e3a\u4e86\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u90fd\u4f7f\u7528\u7edf\u4e00\u7684\u5168\u5c40\u5bf9\u8c61\uff0c\u5f15\u5165\u4e86 globalThis \u3002<\/p>\n<pre>\/\/&nbsp;\u6d4f\u89c8\u5668\nwindow&nbsp;==&nbsp;globalThis&nbsp;\/\/&nbsp;true\n\n\/\/&nbsp;node.js\nglobal&nbsp;==&nbsp;globalThis&nbsp;\/\/&nbsp;true<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>BigInt<\/strong><\/p>\n<hr>\n<p>JavaScript \u4e2d\u80fd\u591f\u7cbe\u786e\u8868\u8fbe\u7684\u6700\u5927\u6570\u5b57\u662f 2^53 &#8211; 1\u3002\u800c BigInt \u53ef\u4ee5\u7528\u6765\u521b\u5efa\u66f4\u5927\u7684\u6570\u5b57<\/p>\n<pre>const&nbsp;theBiggerNumber&nbsp;=&nbsp;9007199254740991n\nconst&nbsp;evenBiggerNumber&nbsp;=&nbsp;BigInt(9007199254740991)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<hr>\n<p>\u6211\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u60a8\u6709\u7528\uff0c\u5e76\u50cf\u6211\u4e00\u6837\u671f\u5f85 JavaScript \u5373\u5c06\u5230\u6765\u7684\u65b0\u7279\u6027\u3002\u5982\u679c\u60f3\u4e86\u89e3\u66f4\u591a\uff0c\u53ef\u4ee5\u770b\u770b tc39 \u59d4\u5458\u4f1a\u7684\u5b98\u65b9\u3002<\/p>\n<p>\u63a8\u8350\u6559\u7a0b\uff1a\u300a\u300b<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fECMAScript 2020 \u7684\u65b0\u7279\u6027\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>javascript \u662f\u6700\u6d41\u884c\u7684\u7f16\u7a0b\u8bed\u8a00\u4e4b\u4e00\uff0c\u6bcf\u5e74\u90fd\u4f1a\u6dfb\u52a0\u65b0\u7684\u7279\u6027\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u6dfb\u52a0\u5728 ecmascript 2020\uff08\u53c8\u79f0es11\uff09\u4e2d\u7684\u65b0\u7279\u6027\u3002 \u5728\u5f15\u5165 ECMAScript 2015\uff08\u53c8\u79f0 ES6\uff09\u4e4b\u524d\uff0cJavaScript \u53d1\u5c55\u7684\u975e\u5e38\u7f13\u6162\u3002\u4f46\u81ea 2015 \u5e74\u8d77\uff0c\u6bcf\u5e74\u90fd\u6709\u65b0\u7279\u6027\u6dfb\u52a0\u8fdb\u6765\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u662f\u6240\u6709\u7279\u6027\u90fd\u88ab\u73b0\u4ee3\u6d4f\u89c8\u5668\u652f\u6301\uff0c\u4f46\u662f\u7531\u4e8e JavaScript \u7f16\u8bd1\u5668 Babel \u7684\u5b58\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u53ef\u4ee5\u4f7f\u7528\u65b0\u7279\u6027\u4e86\u3002\u672c\u6587\u5c06\u4ecb\u7ecd ECMAScript 2020\uff08ES11\uff09\u7684\u4e00\u4e9b\u6700\u65b0\u7279\u6027\u3002 Optional Chaining \u53ef\u9009\u94fe\u5f0f\u8c03\u7528 \u5927\u90e8\u5206\u5f00\u53d1\u8005\u90fd\u9047\u5230\u8fc7\u8fd9\u4e2a\u95ee\u9898\uff1a TypeError: Cannot read property \u2018x\u2019 of undefined \u8fd9\u4e2a\u9519\u8bef\u8868\u793a\u6211\u4eec\u6b63\u5728\u8bbf\u95ee\u4e00\u4e2a\u4e0d\u5c5e\u4e8e\u5bf9\u8c61\u7684\u5c5e\u6027\u3002 \u8bbf\u95ee\u5bf9\u8c61\u7684\u5c5e\u6027 const&nbsp;flower&nbsp;=&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;colors:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;red:&nbsp;true &nbsp;&nbsp;&nbsp;&nbsp;} } console.log(flower.colors.red)&nbsp;\/\/&nbsp;\u6b63\u5e38\u8fd0\u884c console.log(flower.species.lily)&nbsp;\/\/&nbsp;\u629b\u51fa\u9519\u8bef\uff1aTypeError:&nbsp;Cannot&nbsp;read&nbsp;property&nbsp;&#8216;lily&#8217;&nbsp;of&nbsp;undefined \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cJavaScript \u5f15\u64ce\u4f1a\u50cf\u8fd9\u6837\u629b\u51fa\u9519\u8bef\u3002\u4f46\u662f\u67d0\u4e9b\u60c5\u51b5\u4e0b\u503c\u662f\u5426\u5b58\u5728\u5e76\u4e0d\u91cd\u8981\uff0c\u56e0\u4e3a\u6211\u4eec\u77e5\u9053\u5b83\u4f1a\u5b58\u5728\u3002\u4e8e\u662f\uff0c\u53ef\u9009\u94fe\u5f0f\u8c03\u7528\u5c31\u6d3e\u4e0a\u7528\u573a\u4e86\uff01 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7531\u4e00\u4e2a\u95ee\u53f7\u548c\u4e00\u4e2a\u70b9\u7ec4\u6210\u7684\u53ef\u9009\u94fe\u5f0f\u64cd\u4f5c\u7b26\uff0c\u53bb\u8868\u793a\u4e0d\u5e94\u8be5\u5f15\u53d1\u9519\u8bef\u3002\u5982\u679c\u6ca1\u6709\u503c\uff0c\u5e94\u8be5\u8fd4\u56de undefined\u3002 console.log(flower.species?.lily)&nbsp;\/\/&nbsp;\u8f93\u51fa&nbsp;undefined \u767b\u5f55\u540e\u590d\u5236 \u5f53\u8bbf\u95ee\u6570\u7ec4\u6216\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u53ef\u9009\u94fe\u5f0f\u8c03\u7528\u3002 \u8bbf\u95ee\u6570\u7ec4 let&nbsp;flowers&nbsp;=&nbsp;&nbsp;[&#8216;lily&#8217;,&nbsp;&#8216;daisy&#8217;,&nbsp;&#8216;rose&#8217;] console.log(flowers[1])&nbsp;\/\/&nbsp;\u8f93\u51fa\uff1adaisy flowers&nbsp;=&nbsp;null console.log(flowers[1])&nbsp;\/\/&nbsp;\u629b\u51fa\u9519\u8bef\uff1aTypeError:&nbsp;Cannot&nbsp;read&nbsp;property&nbsp;&#8216;1&#8217;&nbsp;of&nbsp;null console.log(flowers?.[1])&nbsp;\/\/&nbsp;\u8f93\u51fa\uff1aundefined \u767b\u5f55\u540e\u590d\u5236 \u8c03\u7528\u51fd\u6570 let&nbsp;plantFlowers&nbsp;=&nbsp;()&nbsp;=&amp;gt;&nbsp;{ &nbsp;&nbsp;return&nbsp;&#8216;orchids&#8217; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-33275","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/33275","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=33275"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/33275\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=33275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=33275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=33275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}