ECMAScript 2023 现已获得 ECMA International 的批准。ECMAScript 是标准化的 JavaScript 语言,于 1997 年发布了第一版,现已发展成为世界上使用最广泛的通用编程语言之一。
本 Ecma 标准定义了 ECMAScript 2023 Language,是 ECMAScript 语言规范的第 14 版。
ECMAScript 2023,第 14 版,在Array.prototype和TypedArray.prototype上引入了toSorted、toReversed、with、findLast和findLastIndex方法,以及Array.prototype上的toSpliced方法;在文件开头增加了对#!注释的支持,以更好地促进 ECMAScript 文件的可执行;并允许在弱集合中使用大多数 Symbols 作为 keys 。
![]()
最终提案由 ECMA TC39 在 GitHub 上发布,详细阐述了今年将发布的四个功能:
Array find from last
该提案在Array和TypedArray原型上增加了findLast()和findLastIndex() 方法。这与 Array.prototype.find 和 Array.prototype.findIndex 的行为相同,但会从最后一个迭代到第一个
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
// ======== Before the 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
// ======== In the 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 Grammar
Hashbang,也称为 shebang,是可执行脚本开头的字符序列,用于定义要运行的程序的解释器。当 Unix 内核的程序加载器执行 JavaScript 程序时,主机会剥离 hashbang 以生成有效源,然后再将其传递给引擎。Hashbang Grammar 提案标准化了它的完成方式。
#!/usr/bin/env node
// in the Script Goal
'use strict';
console.log(1);
#!/usr/bin/env node
// in the Module Goal
export {};
console.log(1);
Symbols as WeakMap keys
该提案扩展了 WeakMap API,以允许使用 unique Symbols 作为 keys。
const weak = new WeakMap();
// Pun not intended: being a symbol makes it become a more symbolic key
const key = Symbol('my ref');
const someObject = { /* data data data */ };
weak.set(key, someObject);
Change Array by Copy
在Array.prototype和TypedArray.prototype上提供了额外的方法,通过返回一个带有变化的新 copy 来实现对数组的改变。
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]
具体可查看: