ES2019新特性你知道哪些?
What’s new in ECMAScript 2019 (ES2019) / ES10
Object.fromEntries()
Array 、Map —> Object
const map = new Map([ ['foo', 'bar'], ['baz', 42] ]);
const obj = Object.fromEntries(map);
console.log(obj); // { foo: "bar", baz: 42 }
const arr = [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ];
const obj = Object.fromEntries(arr);
console.log(obj); // { 0: "a", 1: "b", 2: "c" }
Array.prototype.flat
let arr = [1, 2, 3, [4, 5, 6, [7, 8, 9, [10, 11, 12]]]];
arr.flat(); // [1, 2, 3, 4, 5, 6, Array(4)];
arr.flat().flat(); // [1, 2, 3, 4, 5, 6, 7, 8, 9, Array(3)];
arr.flat(3); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
arr.flat(Infinity); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Array.prototype.flatMap
先map在flat
let grades = [78, 62, 80, 64]
let flatMapped = grades.flatMap(grade => [grade, grade + 7]);
// [78, 85, 62, 69,80, 87, 64, 71]
String.trimStart 和 String.trimEnd
String.trimRight 和 String.trimLeft 效果相同
更符合语义
catch参数可选
try {
...
} catch(e) {
...
}
try {
...
} catch {
...
}
Symbol.prototype.description
Symbol('mySymbol').description; // mySymbol