{"id":1314,"date":"2022-06-24T22:09:27","date_gmt":"2022-06-24T14:09:27","guid":{"rendered":"https:\/\/zhuxinyong.com\/?p=1314"},"modified":"2022-06-26T00:47:42","modified_gmt":"2022-06-25T16:47:42","slug":"es2020es11-zhong-ni-xu-yao-zhi-dao-de-8-ge-te-xing","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1314","title":{"rendered":"42 &#8211; ES2020\/ES11 \u4e2d\u4f60\u9700\u8981\u77e5\u9053\u7684 8 \u4e2a\u7279\u6027"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/06\/16560809954174.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/8-features-from-es2020-es11-you-should-know-1n9\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/8-features-from-es2020-es11-you-should-know-1n9<\/a><\/p>\n<p>ES2020\uff08ES11\uff09\u89c4\u8303\u4eca\u5e74\uff08\u4f5c\u8005\u5199\u6587\u7ae0\u90a3\u5e74\uff09\u53d1\u5e03\u4e86\uff0c\u5b83\u5f15\u5165\u4e86\u4e0d\u5c11\u65b0\u7279\u6027\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u5b66\u4e60\u4e00\u4e0b\u65b0\u89c4\u8303\u4e2d\u7684 8 \u4e2a\u5173\u952e\u4eae\u70b9\u7279\u6027\u3002<\/p>\n<h2><a id=\"%E5%8A%A8%E6%80%81%E5%BC%95%E5%85%A5\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u52a8\u6001\u5f15\u5165<\/h2>\n<p>Babel \u548c Webpack \u5141\u8bb8\u6211\u4eec\u6709\u6761\u4ef6\u7684\u628a JavaScript \u6587\u4ef6\u4f5c\u4e3a\u6a21\u5757\u5f15\u5165\u5230\u5e94\u7528\u4e2d\u3002\u52a8\u6001\u5f15\u5165\u73b0\u5728\u539f\u751f\u652f\u6301\u4e86\u3002\u8fd9\u4e2a\u7279\u6027\u88ab\u7528\u6765\u63d0\u9ad8\u4ee3\u7801\u5206\u5272\u548c\u6309\u9700\u52a0\u8f7d\u4ee3\u7801\uff08\u5141\u8bb8\u61d2\u52a0\u8f7d\uff09\u3002<\/p>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<p>\u6211\u4eec\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a greeting \u6a21\u5757\uff0c\u63a5\u6536 name \u4f5c\u4e3a\u53c2\u6570\u540c\u65f6\u6253\u5370\u51fa name \u76f8\u5173\u7684\u95ee\u5019\u4fe1\u606f\u3002<\/p>\n<pre><code class=\"language-plain_text\">export const greeting = (name) =&gt; console.log(`Hello ${name}`);\n<\/code><\/pre>\n<p>\u4f60\u53ef\u4ee5\u6709\u6761\u4ef6\u7684\u5f15\u5165\u5230\u5e94\u7528\u4e2d\uff1a<\/p>\n<pre><code class=\"language-plain_text\">const time = &quot;morning&quot;; \/\/ this is dynamically set to the time of day, hardcoded for example\n\nif (time === &quot;morning&quot;) {\n    const say = await import(&quot;.\/greeting.js&quot;);\n    say.greeting(&quot;Parwinder&quot;); \/\/ Hello Parwinder\n}\n<\/code><\/pre>\n<h2><a id=\"%E7%A7%81%E6%9C%89%E7%B1%BB%E5%8F%98%E9%87%8F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u79c1\u6709\u7c7b\u53d8\u91cf<\/h2>\n<p>\u6211\u4e4b\u524d\u7684\u535a\u6587\u4e2d\u63d0\u5230\u8fc7\u79c1\u6709\u53d8\u91cf\u548c\u65b9\u6cd5\uff0c\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u4e0b\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-plain_text\">class ObjectCreator {\n    #meaningOfLife;\n\n    constructor(name) {\n        this.#meaningOfLife = 42;\n    }\n\n    returnMeaningOfLife() {\n        return this.#meaningOfLife;\n    }\n\n    #returnAMessage() {\n        return &quot;You will do great things in life&quot;;\n    }\n}\n\nconst myObject = new ObjectCreator(&quot;Parwinder&quot;);\nconsole.log(myObject.returnMeaningOfLife()); \/\/ 42\nconsole.log(myObject[&quot;#meaningOfLife&quot;]); \/\/ undefined\nconsole.log(myObject.#meaningOfLife); \/\/ SyntaxError\nconsole.log(myObject.#returnAMessage); \/\/ SyntaxError\n<\/code><\/pre>\n<p>\u8bed\u8a00\u5c42\u9762\u8fdb\u884c\u4e86\u5c01\u88c5\uff0c\u5728\u4f5c\u7528\u57df\u4e4b\u5916\u5f15\u7528 <code>#<\/code> \u76f8\u5173\u7684\u5c5e\u6027\u5219\u4f1a\u62a5\u9519\u3002\u5168\u5c40\u548c\u79c1\u6709\u5b57\u6bb5\u4e0d\u4f1a\u51b2\u7a81\uff0c\u5728\u540c\u4e00\u4e2a\u7c7b\u4e2d\u53ef\u4ee5\u540c\u65f6\u5b58\u5728\u79c1\u6709\u6216\u79c1\u6709\u7684 <code>#meaningOfLife<\/code> \u5b57\u6bb5\u3002<\/p>\n<h2><a id=\"%E5%8F%AF%E9%80%89%E9%93%BE\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u53ef\u9009\u94fe<\/h2>\n<p>JavaScript \u4e2d\u83b7\u53d6\u5bf9\u8c61\u5c5e\u6027\u662f\u4e00\u4e2a\u5e38\u89c1\u64cd\u4f5c\uff0c\u591a\u6570\u65f6\u5019\u8fd9\u4e9b\u5c5e\u6027\u662f\u5d4c\u5957\u7684\u3002\u5f53\u4f60\u4ece\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u5bf9\u8c61\u4e0a\u83b7\u53d6\u5c5e\u6027\u65f6 JavaScript \u5c31\u4f1a\u62a5\u9519\u3002<\/p>\n<p><code>?.<\/code> \u64cd\u4f5c\u7b26\u5728\u5bf9\u8c61\u4e0a\u6267\u884c\u5c5e\u6027\u83b7\u53d6\u3002\u5728\u94fe\u5f0f\u83b7\u53d6\u5c5e\u6027\u65f6\uff0c\u53ef\u9009\u94fe\u4e0d\u662f\u629b\u51fa\u9519\u8bef\u800c\u662f\u9047\u5230\u7b2c\u4e00\u4e2a undefined\/null \u65f6\u5c3d\u5feb\u7684\u7ed3\u675f\u5e76\u8fd4\u56de <code>undefined<\/code>\u3002<\/p>\n<pre><code class=\"language-plain_text\">const myObject = {\n    name: &quot;Parwinder&quot;,\n    car: &quot;Cybertruck&quot;,\n    age: 42,\n    computers: {\n        first: {\n            name: &quot;iMac&quot;,\n            year: 2017,\n            spec: {\n                cpu: &quot;i7&quot;,\n                ram: &quot;16GB&quot;\n            }\n        },\n        second: {\n            name: &quot;MacBook Pro&quot;\n        }\n    }\n}\n\nconsole.log(myObject.computers.first.spec.cpu); \/\/ i7\nconsole.log(myObject.computers.second.spec.cpu); \/\/ Cannot read property 'cpu' of undefined\n<\/code><\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u53ef\u9009\u94fe\u6765\u89e3\u51b3 <code>cpu<\/code> \u7684\u8bbf\u95ee\u9519\u8bef\u3002<\/p>\n<pre><code class=\"language-plain_text\">myObject?.computers?.second?.spec?.cpu \/\/ undefined\n\n\/\/ Concise and easy to read code\n\/\/ or\n\nmyObject.computers.second.spec?.cpu\n<\/code><\/pre>\n<h2><a id=\"promise-allsettled\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Promise.allSettled<\/h2>\n<p>\u4e4b\u524d\u6587\u7ae0\u5df2\u7ecf\u4ecb\u7ecd\u8fc7\uff0c\u8fd9\u91cc\u5c31\u53ea\u5199\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-plain_text\">const promise1 = Promise.resolve(&quot;Parwinder&quot;);\nconst promise2 = new Promise((resolve) =&gt; {\n    setTimeout(() =&gt; {\n        resolve(&quot;Lauren&quot;);\n    }, 2000);\n});\nconst promise3 = Promise.reject(&quot;Robert&quot;);\nconst promise4 = Promise.resolve(&quot;Eliu&quot;);\n\nPromise.allSettled([promise1, promise2, promise3, promise4]).then((data) =&gt; {\n    console.log(data);\n});\n<\/code><\/pre>\n<p>\u4e00\u65e6\u4e0a\u9762\u56db\u4e2a promise \u7ed3\u675f\uff0c<code>allSettled<\/code> \u4f1a\u628a\u7ed3\u679c\u4f20\u9012\u5230 then \u7684 callback \u4e2d\uff0c\u6253\u5370\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-plain_text\">[{\n  status: &quot;fulfilled&quot;,\n  value: &quot;Parwinder&quot;\n}, {\n  status: &quot;fulfilled&quot;,\n  value: &quot;Lauren&quot;\n}, {\n  reason: &quot;Robert&quot;,\n  status: &quot;rejected&quot;\n}, {\n  status: &quot;fulfilled&quot;,\n  value: &quot;Eliu&quot;\n}]\n<\/code><\/pre>\n<h2><a id=\"string-prototype-matchall\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>String.prototype.matchAll()<\/h2>\n<p><code>matchAll<\/code> \u662f String \u539f\u578b\u4e0a\u7684\u65b0\u65b9\u6cd5\uff0c\u5b83\u4f7f\u6211\u4eec\u53ef\u4ee5\u628a\u5b57\u7b26\u4e32\u4e0e\u6b63\u5219\u8fdb\u884c\u5339\u914d\uff0c\u7136\u540e\u8fd4\u56de\u4e00\u4e2a iterator \u5bf9\u8c61\u4ee3\u8868\u6240\u6709\u5339\u914d\u7684\u7ed3\u679c\u3002<\/p>\n<pre><code class=\"language-plain_text\">const input = 'Hello Andy, Where is Beth? Emily was asking for her.';\nconst regex = \/[A-E]\/g;\nconst matches = input.matchAll(regex);\n\nconsole.log([...matches][0]); \n\/\/ [ \n\/\/     0: &quot;A&quot;\n\/\/     groups: undefined\n\/\/     index: 6\n\/\/     input: &quot;Hello Andy, Where is Beth? Emily was asking for her.&quot;\n\/\/ ]\n<\/code><\/pre>\n<h2><a id=\"globalthis\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>globalThis<\/h2>\n<p>\u6839\u636e\u4ee3\u7801\u6267\u884c\u73af\u5883\u4e0d\u540c\uff0c\u6211\u4eec\u4f7f\u7528\u4e0d\u540c\u8bed\u6cd5\u6765\u83b7\u53d6\u5168\u5c40\u5bf9\u8c61\uff0c\u5728\u6d4f\u89c8\u5668\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>window<\/code>\u3001<code>self<\/code>\u3001<code>frame<\/code>\uff0c\u4f46\u5728 Web Workers \u4e2d\uff0c\u6211\u4eec\u53ea\u80fd\u4f7f\u7528 <code>self<\/code>\u3002\u5728 Node \u4e2d\u53c8\u5b8c\u5168\u4e0d\u540c\uff0c\u6211\u4eec\u53ea\u80fd\u4f7f\u7528 <code>global<\/code>\u3002<\/p>\n<p><code>globalThis<\/code> \u7684\u76ee\u7684\u5c31\u662f\u63d0\u4f9b\u4e00\u4e2a\u6807\u51c6\u7684\u83b7\u53d6\u5168\u5c40\u5bf9\u8c61\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-plain_text\">console.log(globalThis); \/\/ Window {...} for browsers\nconsole.log(globalThis); \/\/ Object [global] {...} for Node\nconsole.log(globalThis); \/\/ DedicatedWorkerGlobalScope {...} for Web Workers\n<\/code><\/pre>\n<h2><a id=\"bigint\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>BigInt<\/h2>\n<p><code>BigInt<\/code> \u662f\u4e00\u4e2a\u6570\u503c\u7c7b\u578b\uff0c\u4f9b\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4efb\u610f\u957f\u5ea6\u7684\u6574\u6570\uff08\u6bd4 <code>2**53 - 1<\/code> \u6216 9007199254740991 \u8fd8\u5927\u7684\u6570\uff09\u3002<\/p>\n<p>\u6211\u4eec\u901a\u8fc7\u5728\u6574\u6570\u5c3e\u90e8\u6dfb\u52a0 <code>n<\/code> \u6216 \u8c03\u7528 <code>BigInt()<\/code> \u6765\u521b\u5efa <code>BigInt<\/code> \u7c7b\u578b\u6574\u6570\u3002<\/p>\n<pre><code class=\"language-plain_text\">const bigint = 9879846412313194464434496849n;\nconst bigintByMethod = BigInt(&quot;9879846412313194464434496849&quot;);\n\nconsole.log(bigint); \/\/ 9879846412313194464434496849\nconsole.log(bigintByMethod); \/\/ 9879846412313194464434496849\n\nconsole.log(bigint === bigintByMethod); \/\/ true\n\nconsole.log(typeof bigint); \/\/ bigint\nconsole.log(typeof bigintByMethod); \/\/ bigint\n\nconst bigintFromExisting = BigInt(25);\n\nconsole.log(bigintFromExisting); \/\/ 25\nconsole.log(typeof bigintFromExisting); \/\/ bigint\n<\/code><\/pre>\n<h2><a id=\"%E7%A9%BA%E5%80%BC%E5%90%88%E5%B9%B6%E8%BF%90%E7%AE%97%E7%AC%A6\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u7a7a\u503c\u5408\u5e76\u8fd0\u7b97\u7b26<\/h2>\n<p><code>??<\/code> \u8fd0\u7b97\u7b26\u5f53\u5176\u5de6\u4fa7\u7684\u503c\u4e3a <code>null<\/code> \u6216 <code>undefined<\/code> \u65f6\u8fd4\u56de\u5176\u53f3\u4fa7\u7684\u503c\uff0c\u53cd\u4e4b\u8fd4\u56de\u5de6\u4fa7\u7684\u503c\u3002<\/p>\n<pre><code class=\"language-plain_text\">const luckyNumber = 0 ?? 42;\nconsole.log(luckyNumber); \/\/ 0\n\nconst employeeName = null ?? &quot;Parwinder&quot;;\nconsole.log(employeeName); \/\/ Parwinder\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a<code>??<\/code> \u64cd\u4f5c\u7b26\u5728 <code>false<\/code> \u6216 <code>NaN<\/code> \u5e76\u4e0d\u8d77\u4f5c\u7528\uff0c\u8fd9\u5c31\u662f\u5b83\u4e0e <code>||<\/code> \u7684\u533a\u522b\u3002<code>||<\/code> \u603b\u662f\u8fd4\u56de\u53ef\u8f6c\u4e3a\u771f\u503c\u7684\u503c\uff0c\u800c <code>??<\/code> \u64cd\u4f5c\u7b26\u5219\u8fd4\u56de\u975e\u7a7a\u7684\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1ahttps:\/\/dev.to\/bhagatparwinder\/8-features-fro&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,20,3],"tags":[163,164,162],"class_list":["post-1314","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","category-tech","tag-allsettle","tag-dynamic-import","tag-nullish-coalescing"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1314","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1314"}],"version-history":[{"count":2,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1314\/revisions"}],"predecessor-version":[{"id":1316,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1314\/revisions\/1316"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}