ES2023來了!深入解析JavaScript的最新更新
1.從數(shù)組末尾查找元素
這個函數(shù)允許我們根據(jù)條件從數(shù)組的最后一個元素向前查找元素。例如:
const array = [{a: 1, b: 1}, {a: 2, b: 2}, {a: 3, b: 3}, {a: 4, b: 4}]
console.log(array.findLast(n => n)); //result -> {a: 4,b: 4 }
console.log(array.findLast(n => n.a * 5 === 20)); // result -> {a:4,b:4}
console.log(array.findLast(n => n.a * 5 === 21)); //result -> undefined
console.log(array.findLastIndex(n => n.a * 5 === 21)); // result -> -1
console.log(array.findLastIndex(n => n.a * 5 === 20)); // result -> 3
分析:
const array = [{a: 1, b: 1}, {a: 2, b: 2}, {a: 3, b: 3}, {a: 4, b: 4}]
console.log(array.findLast(n => n)); //結(jié)果 -> {a: 4,b: 4 }
使用 findLast 方法從數(shù)組的末尾開始查找第一個滿足條件(n => n,即所有元素)的元素。因?yàn)樗性囟紳M足條件,所以它返回了數(shù)組的最后一個元素 {a: 4, b: 4}。
console.log(array.findLast(n => n.a * 5 === 20)); //結(jié)果 -> {a:4,b:4},因?yàn)闂l件為真,所以返回最后一個元素。
使用 findLast 方法從數(shù)組的末尾開始查找第一個滿足條件(n => n.a * 5 === 20)的元素。這個條件相當(dāng)于在查找數(shù)組中 a 屬性值為4的元素,所以它返回了 {a: 4, b: 4}。
console.log(array.findLast(n => n.a * 5 === 21)); //結(jié)果 -> undefined,因?yàn)闂l件為假,所以返回undefined,而不是 {a:4,b:4}。
使用 findLast 方法從數(shù)組的末尾開始查找第一個滿足條件(n => n.a * 5 === 21)的元素。由于數(shù)組中沒有任何元素的 a 屬性值可以滿足這個條件,所以返回 undefined。
console.log(array.findLastIndex(n => n.a * 5 === 21)); //結(jié)果 -> -1,因?yàn)闂l件不能為返回最后一個元素。
使用 findLastIndex 方法從數(shù)組的末尾開始查找第一個滿足條件(n => n.a * 5 === 21)的元素的索引。由于數(shù)組中沒有任何元素的 a 屬性值可以滿足這個條件,所以返回 -1。
console.log(array.findLastIndex(n => n.a * 5 === 20)); //結(jié)果 -> 3,這是最后一個元素的索引,因?yàn)闂l件為真。
使用 findLastIndex 方法從數(shù)組的末尾開始查找第一個滿足條件(n => n.a * 5 === 20)的元素的索引。這個條件相當(dāng)于在查找數(shù)組中 a 屬性值為4的元素的索引,所以它返回了 3,這是數(shù)組的最后一個元素的索引。
2.Hashbang 語法
這個特性使我們能夠在某些命令行接口中使用 Hashbang / Shebang。Shebang 用 #! 表示,是腳本開始的特殊行,告訴操作系統(tǒng)執(zhí)行腳本時應(yīng)該使用哪個解釋器。
#!/usr/bin/env node
// in the Script Goal
'use strict';
console.log(2*3);
#!/usr/bin/env node
// in the Module Goal
export {};
console.log(2*2);
#!/usr/bin/env node 這行代碼將直接調(diào)用一個 Node.js 源文件,作為其自身的可執(zhí)行文件。
我們不需要使用這行代碼 (#!/usr/bin/env node) 來顯式地通過 Node 解釋器調(diào)用文件,例如, node ./file 。
3.將 Symbols 作為 WeakMap 的鍵
這允許使用唯一的 Symbols 作為鍵。目前 WeakMaps 只允許對象作為鍵。因?yàn)樗鼈児蚕硗瑯拥纳矸萏匦浴?/p>
Symbol是ECMAScript中唯一的原始類型,允許使用唯一的值,因此可以使用Symbol作為鍵,而不是創(chuàng)建一個新的帶有WeakMap的對象。
const weak = new WeakMap();
const key = Symbol('my ref');
const someObject = { a:1 };
weak.set(key, someObject);
console.log(weak.get(key));
4.通過復(fù)制改變數(shù)組
這在 Array.prototype 上提供了額外的方法,通過返回帶有更改的新數(shù)組副本,而不是更新原始數(shù)組來更改數(shù)組。
新引入的 Array.prototype 函數(shù)包括:
- Array.prototype.toReversed()
- Array.prototype.toSorted(compareFn)
- Array.prototype.toSpliced(start, deleteCount, ...items)
- Array.prototype.with(index, value)
事例
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
/* toReversed */
const reversed = numbers.toReversed();
console.log("reversed", reversed); // "reversed", [9, 8, 7, 6, 5, 4, 3, 2, 1]
console.log("original", numbers); // "original", [1, 2, 3, 4, 5, 6, 7, 8, 9]
這段代碼使用了 toReversed 方法,該方法返回一個新數(shù)組,新數(shù)組的元素順序與原數(shù)組相反。我們可以看到,原數(shù)組 numbers 并未被改變。
/* toSorted */
const sortedArr = numbers.toSorted();
console.log("sorted", sortedArr); // "sorted", [1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log("original", numbers); // "original", [1, 2, 3, 4, 5, 6, 7, 8, 9]
這段代碼使用了 toSorted 方法,該方法返回一個新數(shù)組,新數(shù)組的元素是原數(shù)組元素的排序結(jié)果。由于原數(shù)組已經(jīng)是排序的,所以新數(shù)組與原數(shù)組相同。我們可以看到,原數(shù)組 numbers 并未被改變。
/* with */
const replaceWith = numbers.with(1, 100);
console.log("with", replaceWith); // "with", [1, 100, 3, 4, 5, 6, 7, 8, 9]
console.log("original", numbers); // "original", [1, 2, 3, 4, 5, 6, 7, 8, 9]
這段代碼使用了 with 方法,該方法返回一個新數(shù)組,新數(shù)組在指定索引位置的元素被替換為指定值。我們可以看到,新數(shù)組的第二個元素已經(jīng)被替換為 100,而原數(shù)組 numbers 并未被改變。
/* toSpliced */
const splicedArr = numbers.toSpliced(0, 4);
console.log("toSpliced", splicedArr); // "toSpliced", [5, 6, 7, 8, 9]
console.log("original", numbers); // "original", [1, 2, 3, 4, 5, 6, 7, 8, 9]
這段代碼使用了 toSpliced 方法,該方法返回一個新數(shù)組,新數(shù)組刪除了原數(shù)組從指定位置開始的指定數(shù)量的元素。我們可以看到,新數(shù)組刪除了從位置0開始的4個元素,而原數(shù)組 numbers 并未被改變。
這些都是ES2023即將推出的一些令人期待的新功能