
系列文章總共分成三篇來介紹,列在下方方便大家參考:
本篇將從 ES2018 接續介紹,還沒看過前一篇的話,可以先看 Part 2 呦!
這一版主要是語法更簡潔、非同步行為更一致。
javascript1for await (const item of asyncIterable) { 2 console.log(item); 3}
javascript1const clone = { ...obj }; 2const { a, ...rest } = obj;
Promise.prototype.finallys (dotAll) flag
javascript1const regex = /f.o/s; 2 3console.log(regex.dotAll); // true
(?<name>...)這一版持續補齊一些常用功能,也對 Array 和 Object 做了更多加強。
Array.prototype.flat:展開陣列
javascript1[1, [2, 3]].flat(); // [1, 2, 3]
Array.prototype.flatMapObject.fromEntries:和 Object.entries 互補。trimStart / trimEndcatch: 省略錯誤參數
javascript1try { 2 ... 3} catch { 4 ... 5}
Function.prototype.toString: 保留原始空白格式。這一版是重要的更新版本,包含不少現代 JavaScript 常用語法。
javascript1user?.address?.city
javascript1value ?? 'default'
Promise.allSettledglobalThis: 在任何 JS 執行環境都能取得全域物件。javascript1const bigInt = BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1); // 9007199254740992n 2const bigIntString = BigInt("9007199254740992"); // 9007199254740992n
javascript1const module = await import('./module.js');
這一版聚焦在字串、Promise 與邏輯運算的新用法。
||=&&=??=String.prototype.replaceAllPromise.any: 只要有一個 Promise 成功就回傳。javascript11_000_000 // = 1000000
這一版對 Class 做了升級,是 Class 系統進化很大的一版。
#field: 私有欄位。#method私有方法。static { ... }: 靜態初始化區塊。await。
javascript1const data = await fetchData();
Object.hasOwn: 比 hasOwnProperty 更安全。/regex/d: RegExp Match Indices,提供更完整的 match 位置資訊。這一版主要是補強 Array 與 Promise,並改善對於資料操作的便利性。
Array.prototype.findLastArray.prototype.findLastIndex#!/usr/bin/env node: Hashbang 支援。Symbol.prototype.descriptionPromise.withResolvers(): 管理 Promise 的新工具。這一版延續近年的趨勢,持續強化陣列原生方法,以及改善資料處理方式。
Array.prototype.toSorted: 不改變原陣列的 sort 方法。Array.prototype.toSplicedArray.prototype.toReversedArray.prototype.with: 回傳更新後的新陣列。
javascript1const arr = [1, 2, 3]; 2const newArr = arr.with(1, 99); // [1, 99, 3]
Object.groupBy / Map.groupBy: 依照 key 來對資料分組。到這裡,我們已經全面了解 ECMAScript 的演進軌跡,從最早的標準到最新年度版本的語法更新與語言改善。這些更新不只豐富了 JavaScript 的功能,也讓開發工作變得更具表達力與可維護性。
雖然本文到此為止,但 ECMAScript 標準仍在每年持續演進。未來的新特性與語法糖將繼續推動語言前行。如果你喜歡這系列內容,可以關注 ECMAScript 的提案流程與更新,進一步理解語言的未來方向。