从ES7 (ECMAScript 2016) 到 ES15 (ECMAScript 2024),JavaScript 每年都会有一些新的特性和改进。以下是这些版本中的一些重要更新:
ES7 (2016)
- 指数运算符 (
**) : 提供了一个更简洁的方式来执行幂运算,如2 ** 10等同于Math.pow(2, 10)。
ES8 (2017)
- Async/Await: 使得异步编程更加接近同步流程控制。
- Object.values/Object.entries: 新增方法用于获取对象的所有值或键值对。
- String padding:
String.prototype.padStart()和String.prototype.padEnd()方法。 - SharedArrayBuffer: 允许共享缓冲区以实现高效的数据交换。
ES9 (2018)
- Async Iterators and Generators: 异步迭代器和生成器。
- Object.getOwnPropertyDescriptors: 获取对象所有属性描述符的方法。
- Array.prototype.flat/flatMap: 执行深层扁平化数组或映射并扁平化。
ES10 (2019)
- Optional Catch Binding: 在
catch块中可以选择性地绑定错误。 - FromEntries:
Object.fromEntries可以将键值对转换成对象。 - BigInt: 支持任意精度的整数。
- Symbol.description: 获取
Symbol的描述。
ES11 (2020)
- Promise.allSettled: 当所有 promise 都已解决时返回一个 promise。
- String.prototype.matchAll: 返回字符串中所有匹配的迭代器。
- WeakRefs: 引入弱引用。
- Dynamic Import: 动态导入模块。
ES12 (2021)
- Logical Assignment Operators: 逻辑赋值运算符 (
&&=和||=). - String.prototype.replaceAll: 替换所有匹配项。
- Promise.any: 返回第一个解决的 promise。
- GlobalThis: 访问全局对象的一个标准方式。
ES13 (2022)
- Error Causation: 错误堆栈跟踪的改进。
- Class Fields: 类字段的私有性和静态性。
- Top-level await: 允许在模块顶层使用
await。
ES14 (2023)
- Record and Tuple Types: 记录和元组类型,提供更严格的类型安全。
- New Array Methods: 如
at方法,用于访问数组中的元素。
ES15 (2024)
- Temporal: 时间处理的新 API,改进了 Date 对象的功能。
- Separate Array Sort: 数组排序的稳定性改进。
- Error Cause: 更加丰富的错误原因追踪。
- Weak References: 进一步增强弱引用的支持。
- New String Methods: 新的字符串方法,如
replaceAll的改进等。
请注意,随着每年新的 ECMAScript 标准发布,浏览器和其他 JavaScript 运行环境的支持程度可能会有所不同。在使用新特性时,建议检查目标环境的兼容性。如果你正在使用 TypeScript 或 Vue.js,通常这些框架和库会跟随最新的 JavaScript 标准,并且可能提供额外的工具或语法糖来帮助使用这些新特性。
ES7 (2016)
-
指数运算符 (
**) :const power = 2 ** 10; // 1024
ES8 (2017)
-
Async/Await:
async function fetchUser() { const response = await fetch('https://api.example.com/user'); const user = await response.json(); return user; } -
Object.values/Object.entries:
const obj = { a: 1, b: 2 }; console.log(Object.values(obj)); // [1, 2] console.log(Object.entries(obj)); // [['a', 1], ['b', 2]] -
String padding:
const str = '42'.padStart(5, '0'); // '00042'
ES9 (2018)
-
Async Iterators and Generators:
async function* generateNumbers() { for (let i = 0; i < 5; i++) { await new Promise(resolve => setTimeout(resolve, 1000)); yield i; } } (async () => { for await (const num of generateNumbers()) { console.log(num); } })(); -
Array.prototype.flat/flatMap:
const nestedArray = [1, [2, [3, 4], 5], 6]; const flatArray = nestedArray.flat(2); // [1, 2, 3, 4, 5, 6]
ES10 (2019)
-
Optional Catch Binding:
try { throw new Error('Oops!'); } catch { console.log('An error occurred'); } -
BigInt:
const bigIntValue = 9007199254740991n + 1n; // 9007199254740992n
ES11 (2020)
-
Promise.allSettled:
const promises = [Promise.resolve(1), Promise.reject(2)]; Promise.allSettled(promises).then(results => { results.forEach(result => console.log(result.status)); }); -
String.prototype.matchAll:
const regex = /a/g; const string = 'banana'; const matches = string.matchAll(regex); for (const match of matches) { console.log(match[0]); }
ES12 (2021)
-
Logical Assignment Operators:
let x = true; x ||= false; // x is now true x &&= false; // x is now false -
String.prototype.replaceAll:
const str = 'hello world'; const result = str.replaceAll('l', 'L'); // 'heLLo worLd'
ES13 (2022)
-
Class Fields:
class MyClass { #privateField = 'private value'; // 私有字段 static #staticPrivateField = 'static private value'; // 静态私有字段 } -
Top-level await:
import { sleep } from 'sleep-pkg'; await sleep(1000); // 等待1秒
ES14 (2023)
-
Record and Tuple Types (TypeScript 特性):
type Point = [number, number]; const origin: Point = [0, 0]; -
New Array Methods:
const arr = [1, 2, 3]; console.log(arr.at(-1)); // 3
ES15 (2024)
-
Temporal:
const date = new Temporal.PlainDate(2024, 10, 10); console.log(date); -
Separate Array Sort:
const arr = [3, 1, 2]; const sortedArr = arr.sort({ compare: (a, b) => a - b, stable: true }); console.log(sortedArr); // [1, 2, 3]