# 展望ES2019，8个新特性

2019-02-18 · xiejiahe 2019到啦，来看看已完成的提案！

## 新特性 🎉🎉🎉

• Optional catch binding
• JSON superset
• Symbol.prototype.description
• Function.prototype.toString revision
• Object.fromEntries
• JSON.stringify
• String.prototype.{trimStart,trimEnd}
• Array.prototype.{flat,flatMap} ## 0x01 Optional catch binding

``````let parseResult = someFallbackValue;
try {
parseResult = JSON.parse(potentiallyMalformedJSON);
} catch (e) {

}
``````

``````let parseResult = someFallbackValue;
try {
parseResult = JSON.parse(potentiallyMalformedJSON);
} catch {

}
``````

## 0x03 Symbol.prototype.description

• 直接公开Symbol的[[Description]]内部插槽，而不是间接通过Symbol.prototype.toString。

``````const sym = Symbol('Hello');
const desc = Symbol.prototype.toString.call(sym);
// Symbol(Hello)
``````

``````const sym = Symbol('Hello');
const desc = sym.description
// Hello
``````

## 0x04 Function.prototype.toString revision

``````function App() {/* var */}

App.toString();
// function App() {}
``````

``````function App() {
/* var */
var h = 'hello';
}

App.toString();
// function App() {
//  /* var */
//  var h = 'hello';
// }
``````

## 0x05 Object.fromEntries

``````obj = Object.fromEntries([['a', 0], ['b', 1]]);
// { a: 0, b: 1 }
``````

``````obj = { abc: 1, def: 2, ghij: 3 };
res = Object.fromEntries(
Object.entries(obj)
.filter(([ key, val ]) => key.length === 3)
.map(([ key, val ]) => [ key, val * 2 ])
);

// res is { 'abc': 2, 'def': 4 }
``````

## 0x06 JSON.stringify

``````JSON.stringify('\uD800');
// → '"�"'
``````

``````JSON.stringify('\uD800');
//  '"\\ud800"'

JSON.stringify('\uD800') === '"\\ud800"';
//  true
``````

## 0x07 String.prototype.trimStart / String.prototype.trimEnd

demo：

``````str = '   hello';
str.trimStart();
// 'hello'          为了看的仔细使用单引号括住

str = 'hello   ';
// 'hello'
``````

## 0x08 Array.prototype.{flat, flatMap}

flat 方法会递归到指定深度将所有子数组连接，并返回一个新数组。

``````// depth 可选, 指定嵌套数组中的结构深度，默认值为1
var newArray = arr.flat(depth)
``````

``````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]
``````

flat()方法会移除数组中的空项:

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

flatMap 方法首先使用映射函数映射每个元素，然后将结果压缩成一个新数组。它与 map 和 深度值1的 flat 几乎相同，但 flatMap 通常在合并成一种方法的效率稍微高一些

``````/**
* @param {any} currentValue - 当前正在数组中处理的元素
* @param {Number}  index - 可选的。数组中正在处理的当前元素的索引。
* @param {Array} array - 可选的。被调用的 map 数组
* @param {any} thisArg - 可选的。执行 callback 函数时 使用的this 值
*/
var new_array = arr.flatMap(function callback(currentValue[, index[, array]]) {
// 返回新数组的元素
}[, thisArg])
``````

``````var arr1 = [1, 2, 3, 4];

arr1.map(x => [x * 2]);
// [, , , ]

arr1.flatMap(x => [x * 2]);
// [2, 4, 6, 8]

// 只会将 flatMap 中的函数返回的数组 “压平” 一层
arr1.flatMap(x => [[x * 2]]);
// [, , , ]
``````

``````let arr = ["今天天气不错", "", "早上好"]

arr.map(s => s.split(""))
// [["今", "天", "天", "气", "不", "错"],[""],["早", "上", "好"]]

arr.flatMap(s => s.split(''));
// ["今", "天", "天", "气", "不", "错", "", "早", "上", "好"]
``````

JavaScript
{"ft_lang_label":"__label__zh","ft_lang_prob":0.62641394,"math_prob":0.9732365,"size":3920,"snap":"2023-40-2023-50","text_gpt3_token_len":1953,"char_repetition_ratio":0.11823289,"word_repetition_ratio":0.06827309,"special_character_ratio":0.35765305,"punctuation_ratio":0.2651622,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9676498,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,6,null,6,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-26T02:10:54Z\",\"WARC-Record-ID\":\"<urn:uuid:78809a85-eec7-4d0f-b97b-01fdf8d75b4e>\",\"Content-Length\":\"235608\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:9d544317-78f5-480a-96bd-c95bc81ce467>\",\"WARC-Concurrent-To\":\"<urn:uuid:d5afeebf-b5a8-4869-9d24-c4a2bb03b5bc>\",\"WARC-IP-Address\":\"106.52.62.70\",\"WARC-Target-URI\":\"https://www.xiejiahe.com/blog/detail/5c6acf0f2bbcb67b4a5f3ed1/\",\"WARC-Payload-Digest\":\"sha1:CKBETA7DXTVU3EBUSQZI5RX4ES4UNFUD\",\"WARC-Block-Digest\":\"sha1:5CVU72S2YOWU6WSF4E7VWXT5ILH35NIA\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510130.53_warc_CC-MAIN-20230926011608-20230926041608-00805.warc.gz\"}"}