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

ECMAScript2023(ES14)新特性

概述

ECMAScript2023 于2023年6月27日正式发布, 本文会介绍ECMAScript2023(ES14),即ECMAScript的第14个版本的新特性。

以下摘自官网:ecma-262

ECMAScript 2023, the 14th edition, introduced the toSorted, toReversed, with, findLast, and findLastIndex methods on Array.prototype and TypedArray.prototype, as well as the toSpliced method on Array.prototype; added support for #! comments at the beginning of files to better facilitate executable ECMAScript files; and allowed the use of most Symbols as keys in weak collections.

ES2023新增的特性如下:

  • Array.prototype.findLast/Array.prototype.findLastIndex
  • 数组的拷贝修改:toReversed/toSorted/toSpliced/with
  • shebang(#!)支持
  • 允许使用Symbol作为WeakMap的键

findLast/findLastIndex

findLast方法用于查找数组中最后一个满足条件的元素,findLastIndex方法用于查找数组中最后一个满足条件的元素的索引。

const arr = [1, 2, 3, 4, 5];
const lastEven = arr.findLast(num => num % 2 === 0); // 4(从末尾找第一个偶数)const arr = [1, 2, 3, 4, 5];
const lastEvenIndex = arr.findLastIndex(num => num % 2 === 0); // 3(元素4的索引)
兼容性

数组的拷贝修改

ES2023引入了数组的拷贝修改方法,包括toReversedtoSortedtoSplicedwith方法,这些方法都不会修改原始数组,而是返回经过变更的新数组

  • toReversed:返回倒序新数组
const arr = [1, 2, 3];
const reversedArr = arr.toReversed(); // [3, 2, 1]
console.log(arr); // [1, 2, 3](原数组未变)
  • toSorted:返回排序新数组
const arr = [3, 1, 2];
const sortedArr = arr.toSorted(); // [1, 2, 3]
console.log(arr); // [3, 1, 2](原数组未变)
  • toSpliced:返回一个移除或替换元素后的新数组
const arr = [1, 2, 3, 4];
const newArr = arr.toSpliced(1, 2, 5); // 从索引1删除2个元素,插入5 → [1, 5, 4]
console.log(arr); // [1, 2, 3, 4](原数组未变)
  • with:返回一个特定索引处被替换的新数组
const arr = [1, 2, 3];
const newArr = arr.with(1, 4); // [1, 4, 3]
console.log(arr); // [1, 2, 3](原数组未变)
兼容性

支持文件开头的shebang(#!)

ES2023支持在文件开头使用Hashbang(#!)注释,用于指定执行该文件的解释器路径,方便将文件作为可执行文件运行。

#!/usr/bin/env node
console.log('Hello, world!');
兼容性

支持使用Symbol作为WeakMap/WeakSet的键

ES2023允许使用Symbol作为WeakMap/WeakSet的键,这在之前是不支持的。

const sym = Symbol('mySymbol');
const weakMap = new WeakMap();
weakMap.set(sym, 'value');
console.log(weakMap.get(sym)); // 'value'
兼容性
http://www.lryc.cn/news/606069.html

相关文章:

  • C# 基于halcon的视觉工作流-章27-带色中线
  • HTM 5 的离线储存的使用和原理
  • JavaEE初阶1.0
  • 认知绞肉机:个体实践视域下认知暴力与元认知升维的活体实验研究
  • 今日做题练习
  • 记录自己使用gitee和jenkins
  • PHP 核心特性全解析:从实战技巧到高级应用(2)
  • 按键精灵iOS工具元素命令SetText:自动化输入的终极解决方案
  • .NET Core部署服务器
  • Linux网络-------3.应⽤层协议HTTP
  • Java 大视界 -- Java 大数据在智能交通公交客流预测与线路优化中的深度实践(15 城验证,年省 2.1 亿)(373)
  • 快速搭建Node.js服务指南
  • 前端核心技术Node.js(四)——express框架
  • 8,FreeRTOS时间片调度
  • RPA-重塑企业自动化流程的智能引擎
  • 《能碳宝》AI辅助开发系统方案
  • 免费语音识别(ASR)服务深度指南​
  • 深入解析域名并发请求限制与HTTP/2多路复用技术
  • 电脑远程关机的重要性
  • vue3+arcgisAPI4示例:轨迹点模拟移动(附源码下载)
  • 实战教程 ---- Nginx结合Lua实现WAF拦截并可视化配置教程框架
  • 融合数字孪生的智慧能源光伏场站检测系统应用解析
  • 生产管理升级:盘古IMS MES解锁全链路可控可溯,激活制造效率
  • 从 MySQL 迁移到 TiDB:使用 SQL-Replay 工具进行真实线上流量回放测试 SOP
  • 【NLP舆情分析】基于python微博舆情分析可视化系统(flask+pandas+echarts) 视频教程 - 微博评论数据可视化分析-点赞区间折线图实现
  • 保姆级别IDEA关联数据库方式、在IDEA中进行数据库的可视化操作(包含图解过程)
  • 技术速递|GitHub Copilot for Eclipse 迈出重要一步
  • SQL极简函数实战:巧用GREATEST()与LEAST()实现智能数据截断
  • Promise.all Promise.race Promise.any三个对比
  • 【Flask基础②】 | 路由、响应与异常处理