ES2019新特性你知道哪些?
What’s new in ECMAScript 2019 (ES2019) / ES10

Object.fromEntries()

Array 、Map —> Object

  1. const map = new Map([ ['foo', 'bar'], ['baz', 42] ]);
  2. const obj = Object.fromEntries(map);
  3. console.log(obj); // { foo: "bar", baz: 42 }
  4. const arr = [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ];
  5. const obj = Object.fromEntries(arr);
  6. console.log(obj); // { 0: "a", 1: "b", 2: "c" }

Array.prototype.flat

  1. let arr = [1, 2, 3, [4, 5, 6, [7, 8, 9, [10, 11, 12]]]];
  2. arr.flat(); // [1, 2, 3, 4, 5, 6, Array(4)];
  3. arr.flat().flat(); // [1, 2, 3, 4, 5, 6, 7, 8, 9, Array(3)];
  4. arr.flat(3); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  5. arr.flat(Infinity); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

Array.prototype.flatMap

mapflat

  1. let grades = [78, 62, 80, 64]
  2. let flatMapped = grades.flatMap(grade => [grade, grade + 7]);
  3. // [78, 85, 62, 69,80, 87, 64, 71]

String.trimStart 和 String.trimEnd

String.trimRight 和 String.trimLeft 效果相同
更符合语义

catch参数可选

  1. try {
  2. ...
  3. } catch(e) {
  4. ...
  5. }
  6. try {
  7. ...
  8. } catch {
  9. ...
  10. }

Symbol.prototype.description

  1. Symbol('mySymbol').description; // mySymbol