JavaScript更新到了es13了。2022年6月22日,第123屆Ecma大會批準(zhǔn)了ECMAScript2022語言規(guī)范,這意味著它現(xiàn)在正式成為JavaScript標(biāo)準(zhǔn);而ECMAScript2022是第13次迭代,因此也可稱為ECMAScript13,簡稱ES13。
前端(vue)入門到精通課程:進(jìn)入學(xué)習(xí)
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API調(diào)試工具:點擊使用
本教程操作環(huán)境:windows7系統(tǒng)、ECMAScript 13版、Dell G3電腦。
新的 ES13 規(guī)范終于發(fā)布了。
JavaScript 不是一種開源語言,它是一種需要遵循 ECMAScript 標(biāo)準(zhǔn)規(guī)范編寫的語言,TC39 委員會負(fù)責(zé)討論和批準(zhǔn)新功能的發(fā)布, 那TC39他們是誰?
“ECMA International 的 TC39 是一群 JavaScript 開發(fā)人員、實施者、學(xué)者等,他們與社區(qū)合作維護(hù)和發(fā)展 JavaScript 的定義?!?— TC39.es
他們的發(fā)布過程由五個階段組成,自 2015 年以來,他們一直在進(jìn)行年度發(fā)布,它們通常發(fā)生在春天舉行發(fā)布。
2022 年 6 月 22 日,第 123 屆 Ecma 大會批準(zhǔn)了 ECMAScript 2022 語言規(guī)范,這意味著它現(xiàn)在正式成為標(biāo)準(zhǔn)。
有兩種方法可以引用任何 ECMAScript 版本:
-
按年份:這個新版本將是 ES2022。
-
按其迭代次數(shù):這個新版本將是第 13 次迭代,所以它可以被稱為 ES13。
那么這次這個版本有什么新東西呢?我們可以對哪些功能感到興奮?
01、正則表達(dá)式匹配索引
目前,在 JavaScript 中使用 JavaScript Regex API 時,僅返回匹配的開始索引。但是,對于一些特殊的高級場景,這還不夠。
作為這些規(guī)范的一部分,添加了一個特殊的標(biāo)志 d。通過使用它,正則表達(dá)式 API 將返回一個二維數(shù)組作為名索引的鍵。它包含每個匹配項的開始和結(jié)束索引。如果在正則表達(dá)式中捕獲了任何命名組,它將在 indices.groups 對象中返回它們的開始/結(jié)束索引, 命名的組名將是它的鍵。
// ✅ a regex with a 'B' named group capture const expr = /a+(?<B>b+)+c/d; const result = expr.exec("aaabbbc") // ✅ shows start-end matches + named group match console.log(result.indices); // prints [Array(2), Array(2), groups: {…}] // ✅ showing the named 'B' group match console.log(result.indices.groups['B']) // prints [3, 6]
查看原始提案,https://github.com/tc39/proposal-regexp-match-indices
02、Top-level await
在此提案之前,不接受Top-level await,但有一些變通方法可以模擬這種行為,但其有缺點。
Top-level await 特性讓我們依靠模塊來處理這些 Promise。這是一個直觀的功能。
但是請注意,它可能會改變模塊的執(zhí)行順序, 如果一個模塊依賴于另一個具有Top-level await 調(diào)用的模塊,則該模塊的執(zhí)行將暫停,直到 promise 完成。
讓我們看一個例子:
// users.js export const users = await fetch('/users/lists'); // usage.js import { users } from "./users.js"; // ✅ the module will wait for users to be fullfilled prior to executing any code console.log(users);
在上面的示例中,引擎將等待用戶完成操作,然后,再執(zhí)行 usage.js 模塊上的代碼。
總之,這是一個很好且直觀的功能,需要小心使用,我們不要濫用它。
在此處查看原始提案。https://github.com/tc39/proposal-top-level-await
03、.at( )
長期以來,一直有人要求 JavaScript 提供類似 Python 的數(shù)組負(fù)索引訪問器。而不是做 array[array.length-1] 來做簡單的 array[-1]。這是不可能的,因為 [] 符號也用于 JavaScript 中的對象。
被接受的提案采取了更實際的方法。Array 對象現(xiàn)在將有一個方法來模擬上述行為。
const array = [1,2,3,4,5,6] // ✅ When used with positive index it is equal to [index] array.at(0) // 1 array[0] // 1 // ✅ When used with negative index it mimicks the Python behaviour array.at(-1) // 6 array.at(-2) // 5 array.at(-4) // 3
查看原始提案,https://github.com/tc39/proposal-relative-indexing-method
順便說一句,既然我們在談?wù)摂?shù)組,你知道你可以解構(gòu)數(shù)組位置嗎?
const array = [1,2,3,4,5,6]; // ✅ Different ways of accessing the third position const {3: third} = array; // third = 4 array.at(3) // 4 array[3] // 4
04、可訪問的 Object.prototype.hasOwnProperty
以下只是一個很好的簡化, 已經(jīng)有了 hasOwnProperty。但是,它需要在我們想要執(zhí)行的查找實例中調(diào)用。因此,許多開發(fā)人員最終會這樣做是很常見的:
const x = { foo: "bar" }; // ✅ grabbing the hasOwnProperty function from prototype const hasOwnProperty = Object.prototype.hasOwnProperty // ✅ executing it with the x context if (hasOwnProperty.call(x, "foo")) { ... }
通過這些新規(guī)范,一個 hasOwn 方法被添加到 Object 原型中,現(xiàn)在,我們可以簡單地做:
const x = { foo: "bar" }; // ✅ using the new Object method if (Object.hasOwn(x, "foo")) { ... }
查看原始提案,https://github.com/tc39/proposal-accessible-object-hasownproperty
05、Error Cause
錯誤幫助我們識別應(yīng)用程序的意外行為并做出反應(yīng),然而,理解深層嵌套錯誤的根本原因,正確處理它們可能會變得具有挑戰(zhàn)性,在捕獲和重新拋出它們時,我們會丟失堆棧跟蹤信息。
沒有關(guān)于如何處理的明確協(xié)議,考慮到任何錯誤處理,我們至少有 3 個選擇:
async function fetchUserPreferences() { try { const users = await fetch('//user/preferences') .catch(err => { // What is the best way to wrap the error? // 1. throw new Error('Failed to fetch preferences ' + err.message); // 2. const wrapErr = new Error('Failed to fetch preferences'); // wrapErr.cause = err; // throw wrapErr; // 3. class CustomError extends Error { // constructor(msg, cause) { // super(msg); // this.cause = cause; // } // } // throw new CustomError('Failed to fetch preferences', err); }) } } fetchUserPreferences();
作為這些新規(guī)范的一部分,我們可以構(gòu)造一個新錯誤并保留獲取的錯誤的引用。 我們只需將對象 {cause: err} 傳遞給 Errorconstructor。
這一切都變得更簡單、標(biāo)準(zhǔn)且易于理解深度嵌套的錯誤, 讓我們看一個例子:
async function fetcUserPreferences() { try { const users = await fetch('//user/preferences') .catch(err => { throw new Error('Failed to fetch user preferences, {cause: err}); }) } } fetcUserPreferences();
了解有關(guān)該提案的