ECMA 2023(ES14) 新特性

ECMAScript 2023 主要包含内容

ECMAScript 2023 于 2023 年 6 月 27 日获得 ECMA International 的批准。

ECMAScript 是标准化的 JavaScript 语言,于 1997 年发布了第一版,现已发展成为世界上使用最广泛的通用编程语言之一。

本 Ecma 标准定义了 ECMAScript 2023 Language,是 ECMAScript 语言规范的第 14 版。

从后向前遍历数组

它们的用法和find()、findIndex()类似,唯一不同的是它们是 从后向前 遍历数组,这两个方法适用于数组 和类数组。

  • findLast() 会返回第一个查找到的元素,如果没有找到,就会返回 undefined;
  • findLastIndex() 会返回第一个查找到的元素的索引。如果没有找到,就会返回 -1;

示例:

js 复制代码
const array = [{ value: 1 }, { value: 2 }, { value: 3 }, { value: 4 }];

array.find((n) => n.value % 2 === 1); // { value: 1 }
array.findIndex((n) => n.value % 2 === 1); // 0

// ======== proposal 之前 ===========

// find
[...array].reverse().find((n) => n.value % 2 === 1); // { value: 3 }

// findIndex
array.length - 1 - [...array].reverse().findIndex((n) => n.value % 2 === 1); // 2
array.length - 1 - [...array].reverse().findIndex((n) => n.value === 42); // should be -1, but 4

// ======== proposal 之后 ===========
// find
array.findLast((n) => n.value % 2 === 1); // { value: 3 }

// findIndex
array.findLastIndex((n) => n.value % 2 === 1); // 2
array.findLastIndex((n) => n.value === 42); // -1

Hashbang

此提案是为了匹配某些允许 Shebangs/Hashbang 的 CLI JS 主机中的实际用法。

目前,此类主机会剥离 hashbang,以便在传递给 JS 引擎之前生成有效的 JS 源文本。这会将剥离转移到发动机上,它确实统一并标准化了剥离的方式。

示例:

js 复制代码
#!/usr/bin/env node
// 在脚本目标中
"use strict";
console.log(1);
js 复制代码
#!/usr/bin/env node
// 在模块目标中
export {};
console.log(1);

使用 Symbol 作为 WeakMap 键

目前,WeakMap 仅允许使用对象作为键,这是 WeakMap 的一个限制。新功能扩展了 WeakMap API,允许使用唯一的 Symbol 作为键

示例:

js 复制代码
const weak = new WeakMap();

// 使用符号使它成为一个更具意义的 key
const key = Symbol("my ref");
const someObject = {
    /* data data data */
};

weak.set(key, someObject);

使用复制的方法更改数组内容

该提案在 Array.prototype 和 TypedArray.prototype 上提供了额外的方法,通过返回包含更改的新副本来启用对数组的更改。

该提案向 Array.prototype 引入了以下函数属性:

  • Array.prototype.toReversed() -> Array
  • Array.prototype.toSorted(compareFn) -> Array
  • Array.prototype.toSpliced(start, deleteCount, ...items) -> Array
  • Array.prototype.with(index, value) -> Array

所有这些方法都保持目标数组不变,并返回执行更改的副本。

toReversed 、 toSorted 和 with 也将被添加到 TypedArrays 中:

  • TypedArray.prototype.toReversed() -> TypedArray
  • TypedArray.prototype.toSorted(compareFn) -> TypedArray
  • TypedArray.prototype.with(index, value) -> TypedArray

示例:

js 复制代码
const sequence = [1, 2, 3];
sequence.toReversed(); // => [3, 2, 1]
sequence; // => [1, 2, 3]

const outOfOrder = new Uint8Array([3, 1, 2]);
outOfOrder.toSorted(); // => Uint8Array [1, 2, 3]
outOfOrder; // => Uint8Array [3, 1, 2]

const correctionNeeded = [1, 1, 3];
correctionNeeded.with(1, 2); // => [1, 2, 3]
correctionNeeded; // => [1, 1, 3]

参考内容

相关推荐
sanxing三省1 分钟前
DeepSeek Harness 到底是什么?标题里最重的词其实不是「DeepSeek」
前端·人工智能·deepseek
jianqiang.xue3 分钟前
把Agent塞进MCU:工具次数和上下文长度怎么卡
java·前端·网络·单片机·esp32·ai编程
前端小万1 小时前
思考,行动,复盘|2026
前端·后端·面试
拖孩2 小时前
# 我又用 AI 做了个小程序:24 天,记录我这辈子认识的人
前端·后端·微信小程序
徐小夕2 小时前
深度拆解 Design Agent:LangChain 出品,AI 设计从「好看」走向「可用」
前端·vue.js·github
用户298698530142 小时前
React 前端实现 Excel 与 HTML 双向转换:基于 WebAssembly 的浏览器端方案
javascript·html·excel
1-1=02 小时前
Vue 表单数据和过滤器
前端·javascript·vue.js
oil欧哟3 小时前
Oil UI 的设计思路与日常使用方式:让 AI 摆脱默认模板并保持多页面风格统一
前端
恋猫de小郭3 小时前
Jetpack Compose 1.13 发布,憋了这么久终有交代了
android·前端·flutter
头发还在的女程序员3 小时前
智慧物业源码解析:房屋档案与业主变更怎样保持历史业务连续
前端·javascript·vue.js·物业管理·小区物业·智慧物业