1. 程式人生 > 其它 >ES10(2019)新特性:Array.flat()和Array.flatMap()、String.trimStart()和String.trimEnd()等

ES10(2019)新特性:Array.flat()和Array.flatMap()、String.trimStart()和String.trimEnd()等

ES10新特性(2019)

行分隔符(U + 2028)和段分隔符(U + 2029)符號現在允許在字串文字中,與jsON匹配
更加友好的 JSON.stringify
新增了Array的flat()方法和flatMap()方法
新增了String的trimStart()方法和trimEnd()方法
Object.fromEntries()
Symbol.prototype.description
String.prototype.matchAll
Function.prototype.toString()現在返回精確字元,包括空格和註釋
簡化try {} catch {},修改 catch 繫結
新的基本資料型別BigInt
globalThis
import()
Legacy RegEx
私有的例項方法和訪問器

1.行分隔符(U + 2028)和段分隔符(U + 2029)符號現在允許在字串文字中,與JSON匹配

以前,這些符號在字串文字中被視為行終止符,因此使用它們會導致SyntaxError異常。

2.更加友好的 JSON.stringify

如果輸入 Unicode 格式但是超出範圍的字元,在原先JSON.stringify返回格式錯誤的Unicode字串。現在實現了一個改變JSON.stringify的第3階段提案,因此它為其輸出轉義序列,使其成為有效Unicode(並以UTF-8表示)

3.新增了Array的flat()方法和flatMap()方法

flat()和flatMap()本質上就是是歸納(reduce) 與 合併(concat)的操作。

Array.prototype.flat()

flat()方法會按照一個可指定的深度遞迴遍歷陣列,並將所有元素與遍歷到的子陣列中的元素合併為一個新陣列返回。

flat()方法最基本的作用就是陣列降維

var arr1 = [1, 2, [3, 4]];
arr1.flat();
// [1, 2, 3, 4]
 
var arr2 = [1, 2, [3, 4, [5, 6]]];
arr2.flat();
// [1, 2, 3, 4, [5, 6]]
 
var arr3 = [1, 2, [3, 4, [5, 6]]];
arr3.flat(2);
// [1, 2, 3, 4, 5, 6]
 
//使用 Infinity 作為深度,展開任意深度的巢狀陣列
arr3.flat(Infinity);
// [1, 2, 3, 4, 5, 6]

其次,還可以利用flat()方法的特性來去除陣列的空項其次,還可以利用flat()方法的特性來去除陣列的空項 vararr4 = [1, 2, , 4, 5]; arr4.flat(); // [1, 2, 4, 5]

var arr1 = [1, 2, 3, 4];
 
arr1.map(x => [x * 2]);
// [[2], [4], [6], [8]]
 
arr1.flatMap(x => [x * 2]);
// [2, 4, 6, 8]
 
// 只會將 flatMap 中的函式返回的陣列 “壓平” 一層
arr1.flatMap(x => [[x * 2]]);
// [[2], [4], [6], [8]]

4.新增了String的trimStart()方法和trimEnd()方法

新增的這兩個方法很好理解,分別去除字串首尾空白字元,這裡就不用例子說聲明瞭。

https://www.tmojm.com 招商加盟網

5.Object.fromEntries()

Object.entries()方法的作用是返回一個給定物件自身可列舉屬性的鍵值對陣列,其排列與使用 for...in 迴圈遍歷該物件時返回的順序一致(區別在於 for-in 迴圈也列舉原型鏈中的屬性)。

而Object.fromEntries()則是Object.entries()的反轉。

Object.fromEntries() 函式傳入一個鍵值對的列表,並返回一個帶有這些鍵值對的新物件。這個迭代引數應該是一個能夠實現@iterator方法的的物件,返回一個迭代器物件。它生成一個具有兩個元素的類似陣列的物件,第一個元素是將用作屬性鍵的值,第二個元素是與該屬性鍵關聯的值。

通過 Object.fromEntries, 可以將 Map 轉化為 Object:

const sym = Symbol('The description');

以前,訪問描述的唯一方法是將符號轉換為字串:

assert.equal(String(sym), 'Symbol(The description)');

現在引入了getter Symbol.prototype.description以直接訪問描述:

assert.equal(sym.description, 'The description');

7.String.prototype.matchAll

matchAll() 方法返回一個包含所有匹配正則表示式及分組捕獲結果的迭代器。 在 matchAll 出現之前,通過在迴圈中呼叫regexp.exec來獲取所有匹配項資訊(regexp需使用/g標誌:

const regexp = RegExp('foo*','g');
const str = 'table football, foosball';
 
while ((matches = regexp.exec(str)) !== null) {
  console.log(`Found ${matches[0]}. Next starts at ${regexp.lastIndex}.`);
  // expected output: "Found foo. Next starts at 9."
  // expected output: "Found foo. Next starts at 19."
}

如果使用matchAll ,就可以不必使用while迴圈加exec方式(且正則表示式需使用/g標誌)。使用matchAll 會得到一個迭代器的返回值,配合 for...of, array spread, or Array.from() 可以更方便實現功能:

const regexp = RegExp('foo*','g');
const str = 'table football, foosball';
let matches = str.matchAll(regexp);
 
for (const match of matches) {
  console.log(match);
}
// Array [ "foo" ]
// Array [ "foo" ]
 
// matches iterator is exhausted after the for..of iteration
// Call matchAll again to create a new iterator
matches = str.matchAll(regexp);
 
Array.from(matches, m => m[0]);
// Array [ "foo", "foo" ]

8.Function.prototype.toString()現在返回精確字元,包括空格和註釋

function /* comment */ foo /* another comment */() {}
 
// 之前不會列印註釋部分
console.log(foo.toString()); // function foo(){}
 
// ES2019 會把註釋一同列印
console.log(foo.toString()); // function /* comment */ foo /* another comment */ (){}
 
// 箭頭函式
const bar /* comment */ = /* another comment */ () => {};
 
console.log(bar.toString()); // () => {}

9.修改catch繫結

在 ES10 之前,我們必須通過語法為 catch 子句繫結異常變數,無論是否有必要。很多時候 catch 塊是多餘的。 ES10 提案使我們能夠簡單的把變數省略掉。

不算大的改動。之前是

try {} catch(e) {} 

現在是

try {} catch {}

10.新的基本資料型別BigInt

現在的基本資料型別(值型別)不止5種(ES6之後是六種)了哦!加上BigInt一共有七種基本資料型別,分別是: String、Number、Boolean、Null、Undefined、Symbol、BigInt