当前位置: 首页 > news >正文

ECMAScript2021(ES12)新特性

概述

ECMAScript2021于2021年6月正式发布, 本文会介绍ECMAScript2021(ES12),即ECMAScript的第12个版本的新特性。

以下摘自官网:ecma-262

ECMAScript 2021, the 12th edition, introduced the replaceAll method for Strings; Promise.any, a Promise combinator that short-circuits when an input value is fulfilled; AggregateError, a new Error type to represent multiple errors at once; logical assignment operators (??=, &&=, ||=); WeakRef, for referring to a target object without preserving it from garbage collection, and FinalizationRegistry, to manage registration and unregistration of cleanup operations performed when target objects are garbage collected; separators for numeric literals (1_000); and Array.prototype.sort was made more precise, reducing the amount of cases that result in an implementation-defined sort order.

ECMAScript2021(ES12)

ES2021的新特性如下:

  • String.prototype.replaceAll
  • Promise.any
  • AggregateError
  • WeakRefFinalizationRegistry
  • 数字字面量分隔符
  • 逻辑赋值运算符
  • Array.prototype.sort优化

字符串 replaceAll方法

replaceAll方法用于替换字符串中所有匹配的子串

const str = 'hello world';
const newStr = str.replaceAll('l', 'L');
console.log(newStr); // 'heLLo worLd'
兼容性

Promise.any方法

Promise.any方法接收一个可迭代对象,返回一个Promise对象。当可迭代对象中的任意一个Promise对象变为fulfilled状态时,Promise.any返回的Promise对象也会变为fulfilled状态。如果可迭代对象中的所有Promise对象都变为rejected状态,则Promise.any返回一个包含错误的AggregateError.

AggregateError也是ES2021新增的内容,用于将多个错误合并为一个错误对象,通常用于Promise.any方法中。

Promise.any方法和Promise.all类似,前者是fulfilled的短路操作,后者是rejected的短路操作。

const promise1 = Promise.reject('失败1');
const promise2 = Promise.resolve('成功2');
const promise3 = Promise.resolve('成功3');Promise.any([promise1, promise2, promise3]).then(result => console.log(result)) // 输出:"成功2"(第一个兑现的).catch(err => console.log(err));
兼容性

WeakRef类 与 FinalizationRegistry

WeakRef类用于创建对对象的弱引用,不会阻止对象被垃圾回收(GC)。这与普通的强引用不同——强引用会让对象始终保存在内存中

const obj = { name: 'Alice' };
const weakRef = new WeakRef(obj);
console.log(weakRef.deref()); // { name: 'Alice' }

FinalizationRegistry类用于注册清理回调函数,当弱引用的对象被垃圾回收时,会调用注册的回调函数。

const registry = new FinalizationRegistry(key => {console.log('对象被垃圾回收', key);
});const obj = { name: 'Alice' };
registry.register(obj, 'obj1');
兼容性

数字字面量分隔符

ES2021允许在数字字面量中使用下划线_作为分隔符,提高长数字的可读性

const num1 = 1_000_000; // 等价于 1000000
const num2 = 0.000_001; // 等价于 0.000001
const num3 = 0b1010_1100; // 二进制数字,等价于 0b10101100
兼容性

逻辑赋值运算符

ES2021引入了逻辑赋值运算符,包括&&=||=??=。这些运算符将逻辑运算符和赋值运算符结合起来,使代码更简洁。

  • &&=:当左侧值为真值时,才会赋值
  • ||=:当左侧值为假值(falsenull0'')时,才赋值
  • ??=:当左侧值为nullundefined时,才赋值,(与 ||= 的区别:不把 0'' 视为 “需要赋值” 的情况
// 旧写法
a = a && b;
a = a || b;
a = a ?? b;// 新写法
a &&= b;
a ||= b;
a ??= b;
兼容性

Array.prototype.sort优化

之前的 sort 方法在某些情况下(如包含相同元素的数组)可能产生依赖于引擎实现的排序结果。ES2021 规范更精确地定义了排序算法的行为,减少了这种 “实现定义” 的场景,使不同 JavaScript 引擎的排序结果更一致。

http://www.dtcms.com/a/305915.html

相关文章:

  • Python深度挖掘:openpyxl与pandas高效数据处理实战指南
  • 网络编程-(网络计算机和网络通信)
  • Orange的运维学习日记--18.Linux sshd安全实践
  • CUDA编程9 - 卷积实践
  • String模拟实现的补充说明
  • 工业计算机ARM-如何实现工业数字化升级EC100!
  • QT跨平台应用程序开发框架(13)—— 绘图API
  • Linux设备驱动架构相关文章
  • @Scope(value = WebApplicationContext.SCOPE_REQUEST)和@Scope(“prototype“)区别
  • SQL 连接类型示例:内连接与外连接
  • 分布式系统:一致性
  • 二叉树(全)
  • InspireFace SDK gRPC 开发详细指导
  • 大厂主力双塔模型实践与线上服务
  • 嵌入式——C语言:内存管理、位运算符、构造数据类型(共用体、枚举)
  • NVIDIA Isaac平台推动医疗AI机器人发展研究
  • 【LeetCode 热题 100】33. 搜索旋转排序数组——(解法二)一次二分
  • ragflow 报错ERROR: [Errno 111] Connection refused
  • 2025年6月电子学会青少年软件编程(C语言)等级考试试卷(三级)
  • LeetCode 面试经典 150_数组/字符串_轮转数组(6_189_C++_中等)(额外数组;转置)
  • 磁盘io查看命令iostat与网络连接查看命令netstat
  • Apache HTTP Server 2.4.50 路径穿越漏洞(CVE-2021-42013)
  • 矩阵指数函数 e^A
  • AR技术赋能航空维修:精度与效率的飞跃
  • 基于Catboost的铁路交通数据分析及列车延误预测系统的设计与实现【全国城市可选、欠采样技术】
  • Three.js 与 WebXR:初识 VR/AR 开发
  • nest generate从入门到实战
  • 6.Origin2021如何绘制Y轴截断图?
  • Java 笔记 封装(Encapsulation)
  • 常见存储卡类型及对比