ES15(ES2024)新特性整理

一、Array.prototype.groupBy() 和 groupByToMap()(分组操作)
  • groupBy():根据回调函数将数组分组为对象。

  • groupByToMap():分组结果保存为 Map 类型。

    const items = [
      { type: 'fruit', name: 'apple' },
      { type: 'vegetable', name: 'carrot' },
      { type: 'fruit', name: 'banana' }
    ];
    
    // 按 type 分组
    items.groupBy(item => item.type);
    // 结果:
    // {
    //   fruit: [{type: 'fruit', name: 'apple'}, {type: 'fruit', name: 'banana'}],
    //   vegetable: [{type: 'vegetable', name: 'carrot'}]
    // }
    
    // 分组为 Map
    items.groupByToMap(item => item.type);

适用场景:数据统计、分类展示。

二、Record 和 Tuple 类型(结构化数据)
  • Record:不可变的键值对集合(类似普通对象,但不可修改)。

  • Tuple:不可变的数组(长度和类型固定)。

    const user = #{
      name: "Alice",
      age: 30
    }; // Record
    
    const coords = #[10, 20]; // Tuple
    user.name = "Bob"; // TypeError(不可修改)

 优势:提升数据不可变性,适合函数式编程。

三、Temporal API(日期时间处理现代化)

  • 替代笨重的 Date 对象,提供更直观的日期时间操作。

    const date = Temporal.Now.plainDateISO(); // 当前日期
    const nextWeek = date.add({ days: 7 }); // 加 7 天

核心功能

  • 时区安全计算

  • 精确的时间段操作(如 Temporal.Duration

四、Decorators 装饰器(正式标准化)
  • 用于装饰类、方法或属性,提供元编程能力(类似 TypeScript 的实验性实现)。

    @logExecutionTime
    class Calculator {
      @memoize
      sum(a, b) {
        return a + b;
      }
    }

用途:AOP(面向切面编程)、日志、缓存等。

五、ArrayBuffer.prototype.transfer()(内存转移)
  • 高效转移 ArrayBuffer 所有权,避免数据拷贝。

    const buffer1 = new ArrayBuffer(8);
    const buffer2 = buffer1.transfer(); // buffer1 变为 detached 状态

优势:提升大数据处理的性能(如 WebAssembly)。

六、正则表达式 v 标志(更强大的 Unicode 支持)
  • 增强 Unicode 属性转义和集合运算。

    // 匹配所有表情符号
    const regex = /[\p{Emoji}]/v;

 

  注:如有缺失,请联系作者或在下方评论,我尽量在第一时间补充上去!!!  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

某公司摸鱼前端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值