JavaScript:ES2019 的新特性

原文鏈接:https://blog.tildeloop.com/posts/javascript-what’s-new-in-es2019

作爲最流行的編程語言和最重要的 Web 開發語言之一,JavaScript 不斷演變,每次迭代都會得到一些新的內部更新。讓我們來看看 ES2019 有哪些新的特性,並加入到我們日常開發中:

 

 

Array.prototype.flat()

Array.prototype.flat() 遞歸地將嵌套數組拼合到指定深度。默認值爲 1,如果要全深度則使用 Infinity 。此方法不會修改原始數組,但會創建一個新數組:


 
  1. const arr1 = [1, 2, [3, 4]];

  2. arr1.flat(); 

  3. // [1, 2, 3, 4]

  4.  
  5. const arr2 = [1, 2, [3, 4, [5, 6]]];

  6. arr2.flat(2); 

  7. // [1, 2, 3, 4, 5, 6]

  8.  
  9. const arr3 = [1, 2, [3, 4, [5, 6, [7, 8]]]];

  10. arr3.flat(Infinity); 

  11. // [1, 2, 3, 4, 5, 6, 7, 8]

flat() 方法會移除數組中的空項:


 
  1. const arr4 = [1, 2, , 4, 5];

  2. arr4.flat(); // [1, 2, 4, 5]

 

Array.prototype.flatMap()

flatMap() 方法首先使用映射函數映射每個元素,然後將結果壓縮成一個新數組。它與 Array.prototype.map 和 深度值爲 1的 Array.prototype.flat 幾乎相同,但 flatMap 通常在合併成一種方法的效率稍微高一些。


 
  1. const arr1 = [1, 2, 3];

  2.  
  3. arr1.map(x => [x * 4]); 

  4. // [[4], [8], [12]]

  5.  
  6. arr1.flatMap(x => [x * 4]); 

  7. // [4, 8, 12]

更好的示例:


 
  1. const sentence = ["This is a", "regular", "sentence"];

  2.  
  3. sentence.map(x => x.split(" ")); 

  4. // [["This","is","a"],["regular"],["sentence"]]

  5.  
  6. sentence.flatMap(x => x.split(" ")); 

  7. // ["This","is","a","regular", "sentence"]

  8.  
  9. // 可以使用 歸納(reduce) 與 合併(concat)實現相同的功能

  10. sentence.reduce((acc, x) => acc.concat(x.split(" ")), []);

 

String.prototype.trimStart() 和 String.prototype.trimEnd()

除了能從字符串兩端刪除空白字符的 String.prototype.trim() 之外,現在還有單獨的方法,只能從每一端刪除空格:


 
  1. const test = " hello ";

  2.  
  3. test.trim(); // "hello";

  4. test.trimStart(); // "hello ";

  5. test.trimEnd(); // " hello";

  • trimStart() :別名 trimLeft(),移除原字符串左端的連續空白符並返回,並不會直接修改原字符串本身。

  • trimEnd() :別名 trimRight(),移除原字符串右端的連續空白符並返回,並不會直接修改原字符串本身。

     

Object.fromEntries

將鍵值對列表轉換爲 Object 的新方法。

它與已有 Object.entries() 正好相反,Object.entries()方法在將對象轉換爲數組時使用,它返回一個給定對象自身可枚舉屬性的鍵值對數組。

但現在您可以通過 Object.fromEntries 將操作的數組返回到對象中。

下面是一個示例(將所有對象屬性的值平方):


 
  1. const obj = { prop1: 2, prop2: 10, prop3: 15 };

  2.  
  3. // 轉化爲鍵值對數組:

  4. let array = Object.entries(obj); 

  5. // [["prop1", 2], ["prop2", 10], ["prop3", 15]]

將所有對象屬性的值平方:


 
  1. array = array.map(([key, value]) => [key, Math.pow(value, 2)]); 

  2. // [["prop1", 4], ["prop2", 100], ["prop3", 225]]

我們將轉換後的數組 array 作爲參數傳入 Object.fromEntries ,將數組轉換成了一個對象:


 
  1. const newObj = Object.fromEntries(array); 

  2. // {prop1: 4, prop2: 100, prop3: 225}

 

可選的 Catch 參數

新提案允許您完全省略 catch() 參數,因爲在許多情況下,您並不想使用它:


 
  1. try {

  2.   //...

  3. } catch (er) {

  4.   //handle error with parameter er

  5. }

  6.  
  7. try {

  8.   //...

  9. } catch {

  10.   //handle error without parameter

  11. }

 

Symbol.description

description 是一個只讀屬性,它會返回 Symbol 對象的可選描述的字符串,用來代替 toString() 方法。


 
  1. const testSymbol = Symbol("Desc");

  2.  
  3. testSymbol.description; // "Desc"

  4.  
  5. testSymbol.toString(); // "Symbol(Desc)"

 

Function.toString()

現在,在函數上調用 toString() 會返回函數,與它的定義完全一樣,包括空格和註釋。

之前:


 
  1. function /* foo comment */ foo() {}

  2.  
  3. foo.toString(); // "function foo() {}"

現在:

foo.toString(); // "function /* foo comment */ foo() {}"

 

JSON.parse() 改進

行分隔符 (\u2028) 和段落分隔符 (\u2029),現在被正確解析,而不是報一個語法錯誤。


 
  1. var str = '{"name":"Bottle\uAnGe"}'

  2. JSON.parse(str)

  3. // {name: "BottleAnGe"}

原文鏈接:https://blog.tildeloop.com/posts/javascript-what’s-new-in-es2019

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章