ECMAScript 2019(ES10) 的新特性总结

简介: ECMAScript 2019(ES10) 的新特性总结

image.png快速通道:

老规矩,先纵览下 ES2019 的新功能:

Array.flat()和Array.flatMap():数组展平

String.trimStart()和String.trimEnd():去掉开头结尾空格文本

String.prototype.matchAll:为所有匹配的匹配对象返回一个迭代器

Symbol.prototype.description:只读属性,回 Symbol 对象的可选描述的字符串

Object.fromEntries():返回一个给定对象自身可枚举属性的键值对数组

可选 Catch

JSON Superset 超集

JSON.stringify() 加强格式转化

Array.prototype.sort() 更加稳定

Function.prototype.toString() 重新修订

Array.flat()和Array.flatMap()image.png

Array.flat()把数组展平,通过传入层级深度参数(默认为1),来为下层数组提升层级。如果想提升所有层级可以写一个比较大的数字甚至是Infinity,但不推荐这么做。

[1, 2, [3, 4]].flat();
// [ 1, 2, 3, 4 ]
[1, 2, [3, 4, [5, 6]]].flat(2);
// [ 1, 2, 3, 4, 5, 6 ]

Array.prototype.flatMap() 它是Array.prototype.map() 和 Array.prototype.flat() 的组合,通过对map调整后的数据尝试展平操作

[1, 2, [3, 4]].flatMap(v => {
  if (typeof v === 'number') {
    return v * 2
  } else {
    return v.map(v => v * 2)
  }
})
// [2, 4, 6, 8]

String.trimStart()和String.trimEnd():去掉开头结尾空格文本

image.png

const string = ' Hello ES2019! ';
string.trimStart();
// 'Hello ES2019! '
string.trimEnd();
// ' Hello ES2019!'


更加方便控制了有没有?

String.prototype.matchAll

image.png

const raw_arr = 'test1  test2  test3'.matchAll((/t(e)(st(\d?))/g));
const arr = [...raw_arr];

image.png

Symbol.prototype.description

image.png'

const symbol = Symbol('This is a Symbol');
symbol;
// Symbol(This is a Symbol)
Symbol.description;
// 'This is a Symbol' 

Object.fromEntries():返回一个给定对象自身可枚举属性的键值对数组

image.png

const entries = [ ['foo', 'bar'] ];
const object = Object.fromEntries(entries);
// { foo: 'bar' }

可选 Catch

在进行try...catch错误处理过程中,如果没有给catch传参数的话,代码就会报错。有时候我们并不关心错误情况,如:

const isValidJSON = json => {
  try {
    JSON.parse(json);
    return true;
  } catch (unusedError) {
    // Unused error parameter
    return false;
  }
};

在新规范中,我们可以省略catch绑定的参数和括号,更加灵活啦。

const isValidJSON = json => {
  try {
    JSON.parse(json);
    return true;
  } catch {
    return false;
  }
};

JSON Superset 超集

image.png'

JSON.parse('"\u2028"');
// SyntaxError
// ES2019
JSON.parse('"\u2028"');
// ''

image.png'

JSON.stringify() 加强格式转化

emoji的字符长度是多少?

' '.length;

image.png

JavaScript将emoji解释为两个字符的原因是:UTF-16将emojis表示为两个代理项的组合。我们的emoji用字符'\uD83D'和'\uDE0E'编码。但是如果试图单独编写这样一个字符,例如'\uD83D',则会认为这是一个无效的文本字符串。在早期版本中,这些字符将替换为特殊字符:

JSON.stringify('\uD83D');
// '"�"'

现在在字符代码之前插入转义字符,结果仍是可读且有效的UTF-8/UTF-16代码:

JSON.stringify('\uD83D');
// '"\\ud83d"'

Array.prototype.sort() 更加稳定

之前,规范允许不稳定的排序算法,如快速排序。

在之前的排序中,可能出现[{a: 1, b: 2}, {a: 1, b: 3}...]、[{a: 1, b: 3}, {a: 1, b: 2}...]等多种情况。

现在所有主流浏览器都使用稳定的排序算法。实际上,这意味着如果我们有一个对象数组,并在给定的键上对它们进行排序,那么列表中的元素将保持相对于具有相同键的其他对象的位置。

let array = [
  {a: 1, b: 2},
  {a: 2, b: 2},
  {a: 1, b: 3},
  {a: 2, b: 4},
  {a: 5, b: 3}
];
array.sort((a, b) => a.a - b.a);
// [{a: 1, b: 2}, {a: 1, b: 3}...] / [{a: 1, b: 3}, {a: 1, b: 2}...]

Function.prototype.toString() 重新修订

从ES2019开始,Function.prototype.toString()将从头到尾返回源代码中的实际文本片段。这意味着还将返回注释、空格和语法详细信息。

function /* a comment */ foo() {}

之前,Function.prototype.toString()只会返回了函数的主体,但没有注释和空格。

foo.toString();
// 'function foo() {}'

现在,函数返回的结果与编写的一致:

image.png


相关文章
|
存储 编译器 C语言
函数指针&&数组指针&&数组传参的本质&&字符指针(进阶篇)
函数指针&&数组指针&&数组传参的本质&&字符指针(进阶篇)
215 0
|
11月前
|
前端开发 JavaScript 开发者
前端开发中的组件化设计与性能优化
【10月更文挑战第7天】前端开发中的组件化设计与性能优化
195 0
|
安全 持续交付 Docker
精通 Docker:简化开发、部署与安全保障
精通 Docker:简化开发、部署与安全保障
104 0
|
SQL 存储 数据库
数据库原理第七章课后题答案(第四版)
数据库原理第七章课后题答案(第四版)
151 0
7-5 sdut-C语言实验-第k小的数
7-5 sdut-C语言实验-第k小的数
103 0
|
前端开发
MVVM LiveData+DataBinding+Lifecycle+ViewModel架构
MVVM LiveData+DataBinding+Lifecycle+ViewModel架构
121 1
|
人工智能
CodeGeeX使用体验
CodeGeeX使用体验
416 0
|
存储 Java 编译器
【详识JAVA语言】抽象类和接口
【详识JAVA语言】抽象类和接口
108 1
|
机器学习/深度学习 算法 TensorFlow
精通 TensorFlow 1.x:1~5(3)
精通 TensorFlow 1.x:1~5(3)
134 0
|
Linux
linux中使用while循环实现循环控制示例
linux中使用while循环实现循环控制示例
177 3

热门文章

最新文章