ES2023新内容概要

其中以数组又双叒叕新增了方法为主。可以更方便的操作数组了。也是因为数组数据处理是日常业务中最广范最多的原因了。

新内容清单:

  • Array.prototype.toSorted

  • Array.prototype.toReversed

  • Array.prototype.with

  • Array.prototype.findLast

  • Array.prototype.findLastIndex

  • Array.prototype.toSpliced

  • 正式的 shebang 支持

  • Symbol 作为 WeakMap 的键

toSorted,toReversed,toSpliced 

这三个方法,与现有的sort, reverse, splice方法功能基本相同,唯一区别是,将会返回处理过后的新数组,而不会修改原数组。

with

with() 方法用于基于索引修改单个成员,并返回一个新的数组

const arr = [1, 2, 3, 4, 5]
const newArr = arr.with(2, 'lenton')
console.log(newArr)
// [1, 2, 'lenton', 4, 5]

findLast,findLastIndex

findLast从数组中获取最后一个匹配的元素。如果找不到匹配元素,则返回 undefined,  findLastIndex工作方式与findLast相同,但它返回的是索引

const arr = [56, 37, 91, 66, 39]
const result = arr.findLast(item => item % 2 === 0)
const resultIndex = arr.findLastIndex(item => item % 2 === 0)
console.log(result, resultIndex)
// 66 3

shebang

Shebang 是旧式 Unix 术语,表示井号后跟一个感叹号:#!

#!/usr/bin/env 
nodeconsole.log("Hello, world!")

上面例子告诉操作系统使用 node 程序来运行此脚本。只需键入 ./hello.js 即可运行它。如果没有 #!是无法运行的。

(关于shebang并为做了解,以上demo代码与说明复制而来。)

Symbol 作为 WeakMap 的键

ES14中的最后一个新功能是Symbol 可以作为 WeakMap 的键使用。在这之前,WeakMap仅允许对象作为键值,新特性更容易创建和共享key。


参考文章:

https://juejin.cn/post/7279719681444528163

2023-10-23 10:29:50 181 0

参与讨论

选择你的头像