ES16(ES2025)新特性

4 阅读2分钟

发布时间:预计 2025 年 6 月 ES16(ES2025)覆盖了日期时间、迭代器、正则、模块导入、集合操作与二进制处理等多个方向,是近几年变化较大的一版。

说明:本文按 TC39 Finished Proposals(当前时间点)整理,具体章节顺序与措辞可能随最终规范编辑稿微调。


1. Temporal(现代日期时间 API)

Temporal 提供了比 Date 更清晰、更可预测的时间模型。

const now = Temporal.Now.zonedDateTimeISO('Asia/Shanghai');
console.log(now.toString());

const birthday = Temporal.PlainDate.from('2000-05-20');
const today = Temporal.Now.plainDateISO();
const age = today.year - birthday.year;

为什么重要

  • 时区处理更准确
  • API 语义更清晰
  • 避免 Date 常见陷阱

2. Iterator Helpers(迭代器增强)

原生迭代器可以直接链式调用常用方法:

function* gen() {
  yield* [1, 2, 3, 4, 5, 6];
}

const result = gen()
  .map(x => x * 2)
  .filter(x => x > 5)
  .take(3)
  .toArray();

console.log(result); // [6, 8, 10]

常用方法

  • map()
  • filter()
  • take() / drop()
  • flatMap()
  • toArray()

3. New Set Methods(Set 集合方法)

集合运算无需手写工具函数:

const a = new Set([1, 2, 3, 4]);
const b = new Set([3, 4, 5, 6]);

console.log(a.union(b));               // Set {1,2,3,4,5,6}
console.log(a.intersection(b));        // Set {3,4}
console.log(a.difference(b));          // Set {1,2}
console.log(a.symmetricDifference(b)); // Set {1,2,5,6}

关系判断:

const x = new Set([1, 2]);
const y = new Set([1, 2, 3]);

console.log(x.isSubsetOf(y));   // true
console.log(y.isSupersetOf(x)); // true
console.log(x.isDisjointFrom(new Set([8, 9]))); // true

4. RegExp 相关增强

RegExp.escape

const keyword = 'a+b(c)?';
const safe = RegExp.escape(keyword);
const re = new RegExp(`^${safe}$`);
console.log(re.test('a+b(c)?')); // true

Duplicate named capture groups

const re = /(?<x>a)|(?<x>b)/;
console.log(re.exec('a')?.groups?.x); // 'a'

RegExp Modifiers

const re = /(?i:hello)\s+world/;
console.log(re.test('HELLO world')); // true

5. Promise.try

统一封装“可能同步抛错、也可能返回 Promise”的函数:

function maybeSync(flag) {
  if (!flag) throw new Error('参数错误');
  return 42;
}

Promise.try(() => maybeSync(true))
  .then(v => console.log(v))
  .catch(err => console.error(err.message));

6. JSON Modules 与 Import Attributes

import config from './config.json' with { type: 'json' };
console.log(config.name);

价值

  • JSON 可直接模块化导入
  • 通过属性声明资源类型,语义更明确

7. Array.fromAsync

把异步可迭代对象转成数组:

async function* fetchPages() {
  yield [1, 2];
  yield [3, 4];
}

const arr = await Array.fromAsync(fetchPages(), page => page.join('-'));
console.log(arr); // ['1-2', '3-4']

8. 其它值得关注的特性

Float16 支持

  • Float16Array
  • DataView 对应读写能力
  • Math.f16round

Error.isError

console.log(Error.isError(new Error('x'))); // true
console.log(Error.isError({ message: 'x' })); // false

Math.sumPrecise

console.log(Math.sumPrecise([0.1, 0.2, 0.3]));

Uint8Array Base64

const bytes = Uint8Array.from([72, 105]);
const b64 = bytes.toBase64();
const back = Uint8Array.fromBase64(b64);

JSON.parse source text access / Iterator Sequencing / Redeclarable global eval vars

  • 提供更细粒度的解析与语言一致性能力
  • 更偏底层语义增强,业务侧感知相对弱于前几项

总结

特性说明重要性
Temporal全新日期时间体系⭐⭐⭐⭐⭐
Iterator Helpers迭代器链式处理⭐⭐⭐⭐⭐
Set 新方法原生集合运算⭐⭐⭐⭐⭐
RegExp.escape / Modifiers正则安全与表达增强⭐⭐⭐⭐
Promise.try统一同步/异步入口⭐⭐⭐⭐
Array.fromAsync异步数据聚合⭐⭐⭐⭐
JSON Modules + Import Attributes资源导入更标准⭐⭐⭐⭐
Float16 / Base64 / Error / Math 增强底层能力补齐⭐⭐⭐

ES16 的核心趋势是:把过去常见的工程实践(时间处理、集合运算、数据导入、迭代处理)进一步标准化与原生化