前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ES10(2019)有哪些更新和新特性?

ES10(2019)有哪些更新和新特性?

作者头像
Tz一号
发布2020-09-10 15:53:39
4870
发布2020-09-10 15:53:39
举报
文章被收录于专栏:Tz一号Tz一号
  • ES10新特性(2019)
    • 行分隔符(U + 2028)和段分隔符(U + 2029)符号现在允许在字符串文字中,与JSON匹配
    • 更加友好的 JSON.stringify
    • 新增了Array的flat()方法和flatMap()方法
    • 新增了String的trimStart()方法和trimEnd()方法
    • Object.fromEntries()
    • Symbol.prototype.description
    • String.prototype.matchAll
    • Function.prototype.toString()现在返回精确字符,包括空格和注释
    • 简化try {} catch {},修改 catch 绑定
    • 新的基本数据类型BigInt
    • globalThis
    • import()
    • Legacy RegEx
    • 私有的实例方法和访问器
代码语言:javascript
复制
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]
 
//使用 Infinity 作为深度,展开任意深度的嵌套数组
arr3.flat(Infinity);
// [1, 2, 3, 4, 5, 6]

  • 其次,还可以利用flat()方法的特性来去除数组的空项其次,还可以利用flat()方法的特性来去除数组的空项 var arr4 = [1, 2, , 4, 5]; arr4.flat(); // [1, 2, 4, 5]
代码语言:javascript
复制
var arr1 = [1, 2, 3, 4];
 
arr1.map(x => [x * 2]);
// [[2], [4], [6], [8]]
 
arr1.flatMap(x => [x * 2]);
// [2, 4, 6, 8]
 
// 只会将 flatMap 中的函数返回的数组 “压平” 一层
arr1.flatMap(x => [[x * 2]]);
// [[2], [4], [6], [8]]

4.新增了String的trimStart()方法和trimEnd()方法

新增的这两个方法很好理解,分别去除字符串首尾空白字符,这里就不用例子说声明了。

5.Object.fromEntries()

Object.entries()方法的作用是返回一个给定对象自身可枚举属性的键值对数组,其排列与使用 for...in 循环遍历该对象时返回的顺序一致(区别在于 for-in 循环也枚举原型链中的属性)。

Object.fromEntries() 则是 Object.entries() 的反转。

Object.fromEntries() 函数传入一个键值对的列表,并返回一个带有这些键值对的新对象。这个迭代参数应该是一个能够实现@iterator方法的的对象,返回一个迭代器对象。它生成一个具有两个元素的类似数组的对象,第一个元素是将用作属性键的值,第二个元素是与该属性键关联的值。

  • 通过 Object.fromEntries, 可以将 Map 转化为 Object:
代码语言:javascript
复制
const sym = Symbol('The description');

  以前,访问描述的唯一方法是将符号转换为字符串:

代码语言:javascript
复制
assert.equal(String(sym), 'Symbol(The description)');

  现在引入了getter Symbol.prototype.description以直接访问描述:

代码语言:javascript
复制
assert.equal(sym.description, 'The description');

7.String.prototype.matchAll

matchAll() 方法返回一个包含所有匹配正则表达式及分组捕获结果的迭代器。 在 matchAll 出现之前,通过在循环中调用regexp.exec来获取所有匹配项信息(regexp需使用/g标志:

代码语言:javascript
复制
const regexp = RegExp('foo*','g');
const str = 'table football, foosball';
 
while ((matches = regexp.exec(str)) !== null) {
  console.log(`Found ${matches[0]}. Next starts at ${regexp.lastIndex}.`);
  // expected output: "Found foo. Next starts at 9."
  // expected output: "Found foo. Next starts at 19."
}

如果使用matchAll ,就可以不必使用while循环加exec方式(且正则表达式需使用/g标志)。使用matchAll 会得到一个迭代器的返回值,配合 for...of, array spread, or Array.from() 可以更方便实现功能:

代码语言:javascript
复制
const regexp = RegExp('foo*','g');
const str = 'table football, foosball';
let matches = str.matchAll(regexp);
 
for (const match of matches) {
  console.log(match);
}
// Array [ "foo" ]
// Array [ "foo" ]
 
// matches iterator is exhausted after the for..of iteration
// Call matchAll again to create a new iterator
matches = str.matchAll(regexp);
 
Array.from(matches, m => m[0]);
// Array [ "foo", "foo" ]

8.Function.prototype.toString()现在返回精确字符,包括空格和注释

代码语言:javascript
复制
function /* comment */ foo /* another comment */() {}
 
// 之前不会打印注释部分
console.log(foo.toString()); // function foo(){}
 
// ES2019 会把注释一同打印
console.log(foo.toString()); // function /* comment */ foo /* another comment */ (){}
 
// 箭头函数
const bar /* comment */ = /* another comment */ () => {};
 
console.log(bar.toString()); // () => {}

9.修改 catch 绑定

代码语言:javascript
复制
在 ES10 之前,我们必须通过语法为 catch 子句绑定异常变量,无论是否有必要。很多时候 catch 块是多余的。 ES10 提案使我们能够简单的把变量省略掉。

不算大的改动。

之前是

代码语言:javascript
复制
try {} catch(e) {} 

现在是

代码语言:javascript
复制
try {} catch {}

10.新的基本数据类型BigInt

现在的基本数据类型(值类型)不止5种(ES6之后是六种)了哦!加上BigInt一共有七种基本数据类型,分别是: String、Number、Boolean、Null、Undefined、Symbol、BigInt

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-07-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 4.新增了String的trimStart()方法和trimEnd()方法
  • 5.Object.fromEntries()
  • 7.String.prototype.matchAll
  • 8.Function.prototype.toString()现在返回精确字符,包括空格和注释
  • 9.修改 catch 绑定
  • 10.新的基本数据类型BigInt
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档