將ECMA-262語法擴展爲JSON超集javascript
對ECMAScript進行了語法更改,容許catch中不寫error捕獲java
原寫法git
try { // 嘗試使用可能沒法實現的Web功能 } catch (unused) { // 支持可能沒法實現的web功能狀況 }
之後能夠這樣寫github
try { // do something } catch { // }
新增一個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"
新增一個Function.prototype.toString方法,返回一個表示當前函數源代碼的字符串api
1.語法數組
function.toString()
2.demo函數
const fun = (name) => { console.log(name) } fun.toString() // "(name) => { console.log(name) }"
新增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 }
防止JSON.stringify返回格式錯誤的Unicode字符串的提議
新增String.prototype.trimStart和String.prototype.trimEnd屬性,移除空白字符,並返回一個新字符串
從一個字符串的開端刪除空白字段,並返回一個新字符串
1.語法
str.trimStart();
2.demo
const str = ' 我願是豬 '; const newStr = str.trimStart(); // '我願是豬 '
從一個字符串的末端刪除空白字段,並返回一個新字符串
1.語法
str.trimEnd();
2.demo
const str = ' 我願是豬 '; const newStr = str.trimEnd(); // ' 我願是豬'
新增Array.prototype.flat和Array.prototype.flatMap屬性,對數組的內含數組進行展開操做並返回一個新數組
!參考資料方法會按照一個可指定的深度遞歸遍歷數組,並將全部元素與遍歷到的子數組中的元素合併爲一個新數組返回
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.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]]