ES2019(ES10)

圖片描述

1、JSON superset

將ECMA-262語法擴展爲JSON超集javascript

動機

  • ECMAScript聲稱JSON是一個子集JSON.parse,可是(由於已經有詳細記錄)這是不正確的,由於JSON字符串能夠包含未轉義的U + 2028 LINE SEPARATOR和U + 2029 PARAGRAPH SEPARATOR字符,而ECMAScript字符串則不能

2、Optional catch binding

對ECMAScript進行了語法更改,容許catch中不寫error捕獲java

動機

  • 此提議引入的語法更改容許catch省略綁定其周圍的括號

原寫法git

try {
  // 嘗試使用可能沒法實現的Web功能
} catch (unused) {
  // 支持可能沒法實現的web功能狀況
}

之後能夠這樣寫github

try {
  // do something
} catch {
  //
}

3、Symbol.prototype.description

新增一個Symbol.prototype.description方法,是一個只讀屬性,它會返回 Symbol 對象的可選描述的字符串web

demochrome

console.log(Symbol('desc').description); // expected output: "desc"

console.log(Symbol.iterator.description); // expected output: "Symbol.iterator"

console.log(Symbol.for('foo').description); // expected output: "foo"

console.log(Symbol('foo').description + 'bar'); // expected output: "foobar"

4、Function.prototype.toString revision

新增一個Function.prototype.toString方法,返回一個表示當前函數源代碼的字符串api

1.語法數組

function.toString()

2.demo函數

const fun = (name) => { console.log(name) }
fun.toString() // "(name) => { console.log(name) }"

5、Object.fromEntries ———— 用於將鍵值對列表轉換爲對象(兼容性有點差,chrome最新的beta版才支持)。

新增Object.fromEntries屬性,用於將鍵值對列表轉換爲對象
1.語法post

const newObject = Object.fromEntries(iterable);
iterable: 相似實現了可迭代協議 Array 或者 Map 或者其它對象的可迭代對象

2.demo

const newObject = Object.fromEntries([['a', 1], ['b', 2]]); // { a: 1, b: 2 }
const map = new Map().set('a', 1).set('b', 2);
const newObject1 = Object.fromEntries(map); // { a: 1, b: 2 }

6、Well-formed JSON.stringify

防止JSON.stringify返回格式錯誤的Unicode字符串的提議

7、String.prototype.{trimStart,trimEnd}

新增String.prototype.trimStart和String.prototype.trimEnd屬性,移除空白字符,並返回一個新字符串

String.prototype.trimStart

從一個字符串的開端刪除空白字段,並返回一個新字符串
1.語法

str.trimStart();

2.demo

const str = '   我願是豬    ';
const newStr = str.trimStart(); // '我願是豬    '

String.prototype.trimEnd

從一個字符串的末端刪除空白字段,並返回一個新字符串
1.語法

str.trimEnd();

2.demo

const str = '   我願是豬    ';
const newStr = str.trimEnd(); // '   我願是豬'

8、Array.prototype.{flat,flatMap}

新增Array.prototype.flat和Array.prototype.flatMap屬性,對數組的內含數組進行展開操做並返回一個新數組

Array.prototype.flat

!參考資料
方法會按照一個可指定的深度遞歸遍歷數組,並將全部元素與遍歷到的子數組中的元素合併爲一個新數組返回

1.語法

const newArray = arr.flat(depth)
depth: 須要展開內層數組的層數,默認爲1

2.demo

  • 展開內層數組
/** 默認參數: */
const array = [1, [2, [3]]];
const array1 = array.flat(); // [1, 2, [3]]
/** 遞歸展平,直到數組再也不包含嵌套數組: */
const arrayInfinity = array.flat(Infinity); // [1, 2, 3]
/** 此處等同於 */
const array2 = array.flat(2);
  • 移除空項
const array = [1, 2, , 3];
const arrayRemove = array.flat(); // [1, 2, 3]

Array.prototype.flatMap

方法相似與Array.prototype.map,可是會展開數組(只會展開一層)

1.語法

const new_array = arr.flatMap((currentValue, index, array) => {
    // 返回新數組的元素
})
currentValue: 數組中正在處理的當前元素
index: 正在處理元素的索引
array: 正在被處理素組

2.demo

const array = [1, 2, 3];
const new_array = array.flatMap(ele => [ele * 2]) // [2, 4, 6]
const new_array2 = array.flatMap(ele => [[ele * 2]]) // [[2], [4], [6]]

gitub上修改

本站公眾號
   歡迎關注本站公眾號,獲取更多信息