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.prototypeTypedArray.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

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

toReversedtoSortedwith 也将被添加到 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]

参考内容

相关推荐
多看书少吃饭3 分钟前
Vite开发环境按需编译是怎么实现的
前端
ybb_ymm11 分钟前
@Async修饰不生效
java·前端·数据库
Sapphire~15 分钟前
Vue3-03 熟悉src文件夹及Vue文件格式
前端·javascript·vue.js
快乐星球37219 分钟前
鸿蒙5、6用户h5页面使用schemeURL跳转小程序失败
前端
ChangYan.34 分钟前
Electron使用ffi-napi报错External buffers are not allowed解决办法
前端·javascript·electron
Sept94038 分钟前
详解实现属性的全面拦截
前端
墨渊君39 分钟前
2025 年: 一半无业游民、一半外包牛马
前端·年终总结
借个火er39 分钟前
从零搭建 Uniapp 企业级项目模板
前端
阿民_armin41 分钟前
移动端长列表「返回原位置」的完整实践
前端·javascript·vue.js
Arnbit1on41 分钟前
使用docxtemplater进行Word文档的自动填充
javascript