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

ECMAScript2024(ES15)新特性

概述

ECMAScript2024于2024年6月26日正式发布, 本文会介绍ECMAScript2024(ES15),即ECMAScript的第15个版本的新特性。

以下摘自官网:ecma-262

ECMAScript 2024, the 15th edition, added facilities for resizing and transferring ArrayBuffers and SharedArrayBuffers; added a new RegExp /v flag for creating RegExps with more advanced features for working with sets of strings; and introduced the Promise.withResolvers convenience method for constructing Promises, the Object.groupBy and Map.groupBy methods for aggregating data, the Atomics.waitAsync method for asynchronously waiting for a change to shared memory, and the String.prototype.isWellFormed and String.prototype.toWellFormed methods for checking and ensuring that strings contain only well-formed Unicode.

ECMAScript2024的新特性如下:

  • Group By分组:Object.groupByMap.group
  • Promise.withResolvers
  • 正则表达式标志 /v
  • ArrayBuffersSharedArrayBuffers
  • String.prototype.isWellFormed/toWellFormed
  • Atomics.waitAsync()

Group By分组

ES2024引入Object.groupByMap.groupBy方法就是用于根据指定的条件对对象或可迭代对象进行分组。需要注意的是Object.groupBy方法返回的对象是没有原型的对象,无法继承Object.prototype上的任何属性或方法;另外它的第二个参数回调函数应该返回一个字符串或Symbol类型。

const animals = [{ name: "Lion", type: "Mammal" },{ name: "Shark", type: "Fish" },{ name: "Elephant", type: "Mammal" }
];
// 使用Object.groupBy
const objectGrouped = Object.groupBy(animals, animal => animal.type);
console.log(objectGrouped);
// 输出 { Mammal: [ { name: 'Lion', type: 'Mammal' }, { name: 'Elephant', type: 'Mammal' } ], Fish: [ { name: 'Shark', type: 'Fish' } ] }// 使用Map.groupBy
const mapGrouped = new Map();
Map.groupBy(animals, animal => animal.type, mapGrouped);
console.log(mapGrouped);
// 输出 Map(2) { 'Mammal' => [ { name: 'Lion', type: 'Mammal' }, { name: 'Elephant', type: 'Mammal' } ], 'Fish' => [ { name: 'Shark', type: 'Fish' } ] }
兼容性

Promise.withResolvers

ES2024引入Promise.withResolvers方法,用于创建一个Promise对象,该对象的resolvereject方法可以在任何时候调用。使用示例如下

const { promise, resolve, reject } = Promise.withResolvers();  // 在这里可以使用 resolve 和 reject 函数  
setTimeout(() => resolve('成功!'), 8000);  promise.then(value => {  console.log(value); 
});

其中resolverejectpromise本身是处于同一作用域,而不是在执行器中被创建和一次性使用,这对于更细粒度控制Promise状态非常有用。

兼容性

正则表达式v标志

ES2024提出的v标志是u标志的超集,且v还提供另外两个功能:

  • 字符串的Unicode属性:通过Unicode属性转义,可以使用字符串的属性
  • 设置符号:允许在字符类之间进行集合操作
const str = "hello world";
const regex = /\w+/v;
const match = str.match(regex);
console.log(match); // 输出["hello", "world"]

ArrayBuffersSharedArrayBuffers

ArrayBuffers
  • ArrayBuffer.prototype.resize:用于调整ArrayBuffer的实例大小,以字节为单位,并且该指定的大小不能大于该实例的maxByteLength
  • ArrayBuffer.prototype.transfer:将当前ArrayBuffer的字节复制到一个新的ArrayBuffer对象中
SharedArrayBuffers

SharedArrayBuffer通用可调整大小,但是它只能扩大而不能缩小,而且也不能转移。

// 创建一个可调整大小的ArrayBuffer
const buffer = new ArrayBuffer(8, { maxByteLength: 16 });
buffer.resize(12);
console.log(buffer.byteLength); // 输出12// 创建一个可调整大小的SharedArrayBuffer
const sharedBuffer = new SharedArrayBuffer(8, { maxByteLength: 16 });
sharedBuffer.grow(12);
console.log(sharedBuffer.byteLength); // 输出12// 转移ArrayBuffer所有权
const sourceBuffer = new ArrayBuffer(8);
const targetBuffer = sourceBuffer.transfer();
console.log(sourceBuffer.byteLength); // 输出0
console.log(targetBuffer.byteLength); // 输出8
兼容性

isWellFormed/toWellFormed

  • isWellFormed:用于检查一个UTF-16编码字符串是否包含孤立的代理项
  • toWellFormed:用于将一个UTF-16编码字符串中的孤立代理项替换为替换字符U+FFFD
const badString = '\uD83D\uDCA9\uffff'; // 包含非法字符
console.log(badString.isWellFormed()); // 输出false
const wellFormedString = badString.toWellFormed();
console.log(wellFormedString); // 输出 �\uDCA9\uffff,将非法字符替换为替换字符
兼容性

Atomics.waitAsync

Atomics.waitAsync静态方法异步等待共享内存的特定位置并返回一个Promise

const { SharedArrayBuffer, Atomics } = require('worker_threads');
const sab = new SharedArrayBuffer(4);
const int32Array = new Int32Array(sab);
int32Array[0] = 0;Atomics.waitAsync(int32Array, 0, 0).then(() => {console.log('Shared memory has changed');
});// 在另一个线程或时机修改共享内存
setTimeout(() => {Atomics.store(int32Array, 0, 1);
}, 1000);
兼容性
http://www.xdnf.cn/news/1217395.html

相关文章:

  • SpringAI 1.0.0发布:打造企业级智能聊天应用
  • AI 安监系统:为工业园安全保驾护航
  • 【Debian】4-‌1 Gitea简介以及与其他git方案差异
  • Windows 10 WSLUbuntu 22.04 安装并迁移到 F 盘
  • 2018 年 NOI 最后一题题解
  • 【预判一手面试问题:排序】
  • 2023 年 NOI 最后一题题解
  • n8n为什么建议在数组的每个item中添加json键?
  • Docker部署Nacos
  • LeetCode 53 - 最大子数组和
  • Android Emoji 全面解析:从使用到自定义
  • 《嵌入式C语言笔记(十六):字符串搜索、动态内存与函数指针精要》
  • 企业微信API接口发消息实战:从0到1的技术突破之旅
  • MySQL索引和事务笔记
  • 2419.按位与最大的最长子数组
  • JAVAEE--4.多线程案例
  • Mac配置iterm2
  • 【动态规划 | 多状态问题】动态规划求解多状态问题
  • 信贷风控笔记8-解读商业银行资本管理办法笔记
  • Day 4-1: 机器学习算法全面总结
  • Vue路由钩子完全指南
  • 论文阅读|ArxiV 2024|Mamba进一步研究|VSSD
  • Python Pandas.concat函数解析与实战教程
  • 【力扣热题100】哈希——字母异位词分组
  • 20250730在荣品的PRO-RK3566开发板的Android13下调通敦泰的FT8206触控芯片【I2C的挂载】
  • colima 修改镜像源为国内源
  • 02 基于sklearn的机械学习-KNN算法、模型选择与调优(交叉验证、朴素贝叶斯算法、拉普拉斯平滑)、决策树(信息增益、基尼指数)、随机森林
  • MacTex+Vscode数学建模排版
  • VUE -- 基础知识讲解(二)
  • 计算机网络基础(二) --- TCP/IP网络结构(应用层)