Skip to content

ES14

版本信息

版本介绍

来源: https://262.ecma-international.org/14.0/#sec-intro

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.

版本概要

版本特性

数组方法增强: 数组查找

Array.prototpye.findLast()

findLast() 方法反向迭代数组,并返回满足提供的测试函数的第一个元素的值。如果没有找到对应元素,则返回 undefined

js
const array1 = [5, 12, 50, 130, 44]

const found = array1.findLast((element) => element > 45)

console.log(found)
// 输出: 130

Array.prototpye.findLastIndex()

findLastIndex() 方法反向迭代数组,并返回满足所提供的测试函数的第一个元素的索引。若没有找到对应元素,则返回 -1

索引值是正方向

js
const array1 = [5, 12, 50, 130, 44]

const isLargeNumber = (element) => element > 45

console.log(array1.findLastIndex(isLargeNumber))
// 输出: 3
// 目标值(反向第一个大于45的值)是: 130

数组方法增强:数组拷贝

相关提案:https://github.com/tc39/proposal-change-array-by-copy

Array.prototype.toSorted()

Array 实例的 toSorted() 方法是 sort() 方法的复制方法版本。它返回一个新数组,其元素按升序排列。

js
const months = ['Mar', 'Jan', 'Feb', 'Dec']
const sortedMonths = months.toSorted()
console.log(sortedMonths) // ['Dec', 'Feb', 'Jan', 'Mar']
console.log(months) // ['Mar', 'Jan', 'Feb', 'Dec']

const values = [1, 10, 21, 2]
const sortedValues = values.toSorted((a, b) => a - b)
console.log(sortedValues) // [1, 2, 10, 21]
console.log(values) // [1, 10, 21, 2]

Array.prototype.toReversed()

Array 实例的 toReversed() 方法是 reverse() 方法对应的复制版本。它返回一个元素顺序相反的新数组

toReversed() 方法将调用该方法的数组对象的元素以相反的顺序调换,并返回一个新数组。

当用于稀疏数组时,toReversed() 方法迭代空槽,就像它们的值是 undefined 一样。

toReversed() 方法是通用的。它只期望 this 值具有 length 属性和整数键属性。

js
const items = [1, 2, 3]
console.log(items) // [1, 2, 3]

const reversedItems = items.toReversed()
console.log(reversedItems) // [3, 2, 1]
console.log(items) // [1, 2, 3]

Array.prototype.toSpliced()

Array 实例的 toSpliced() 方法是 splice() 方法的复制版本。它返回一个新数组,并在给定的索引处 删除 和/或 替换了一些元素。

js
toSpliced(start)
toSpliced(start, deleteCount)
toSpliced(start, deleteCount, item1)
toSpliced(start, deleteCount, item1, item2, itemN)

toSpliced() 方法与 splice() 类似,可以同时完成多个操作:在数组中给定的索引开始移除指定数量的元素,然后在相同的索引处插入给定的元素。但是,它返回一个新数组,而不是修改原始数组。因此,此方法不会返回已删除的元素。

js
const months = ['Jan', 'Mar', 'Apr', 'May']

// 在索引 1 处添加一个元素
const months2 = months.toSpliced(1, 0, 'Feb')
console.log(months2) // ["Jan", "Feb", "Mar", "Apr", "May"]

// 从第 2 个索引开始删除两个元素
const months3 = months2.toSpliced(2, 2)
console.log(months3) // ["Jan", "Feb", "May"]

// 在索引 1 处用两个新元素替换一个元素
const months4 = months3.toSpliced(1, 1, 'Feb', 'Mar')
console.log(months4) // ["Jan", "Feb", "Mar", "May"]

// 原数组不会被修改
console.log(months) // ["Jan", "Mar", "Apr", "May"]

Array.prototype.with()

Array 实例的 with() 方法是使用方括号表示法修改指定索引值的复制方法版本。它会返回一个新数组,其指定索引处的值会被新值替换。

js
// 创建一个新的数组,改变其中一个元素
const arr = [1, 2, 3, 4, 5]
console.log(arr.with(2, 6)) // [1, 2, 6, 4, 5]
console.log(arr) // [1, 2, 3, 4, 5]

链式调用

使用 with() 方法,你可以在更新一个数组元素后继续调用其他的数组方法。

js
const arr = [1, 2, 3, 4, 5]
console.log(arr.with(2, 6).map((x) => x ** 2)) // [1, 4, 36, 16, 25]

Hashbang 语法

js
#!/usr/bin/env node
// in the Script Goal

'use strict'
console.log(1)

WeakMap 支持 Symbol 作为 key