ES2019 中的 8 个新特性

版权声明:copyright 少年广坤的烦恼 https://blog.csdn.net/weixin_42437900/article/details/88108088

1.可选的 Catch 绑定

能够在不使用 catch 绑定的地方选择性地删除它

try {
  // trying to use a new ES2019 feature
  // which may not be implemented in other browsers
} catch (unused) {
  // revert back to old way
}

现在可以删除未使用的绑定

try {
  ...
} catch {
  ...
}

2.JSON 超集

此提议的动机是 JSON 字符串可以包含未转义的 U + 2028 LINE SEPARATOR 和 U + 2029 PARAGRAPH SEPARATOR 字符,而 ECMAScript 字符串则不能。在 ES2019 之前,它会产生错误SyntaxError: Invalid or unexpected token

const LS = eval('"\u2028"');
const PS = eval("'\u2029'");

3.符号说明

在 ES2015 中引入符号,具有非常独特的功能。在 ES2019 中,它现在可以提供给定的描述。其目的是避免间接获得所提供的描述Symbol.prototype.toString

const LS = eval('"\u2028"');
const PS = eval("'\u2029'");

4.Function.prototype.toString

我们之前已经在函数原型中使用了toString方法,但是在 ES2019 中它已被修改并包含函数内的注释,请注意它在Arrow Functions上不起作用。

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

// Before
console.log(foo.toString()); // function foo(){}

// Now ES2019
console.log(foo.toString()); // function /* comment */ foo /* another comment */ (){}

// Arrow Syntax
const bar /* comment */ = /* another comment */ () => {};

console.log(bar.toString()); // () => {}

5.Object.fromEntries

它是 Object.entries 的反向方法,它也是克隆对象的方法之一

const obj = {
  prop1: 1,
  prop2: 2
};

const entries = Object.entries(obj);

console.log(entries); // [ [ 'prop1', 1 ], [ 'prop2', 2 ] ]

const fromEntries = Object.fromEntries(entries);

console.log(fromEntries); // Object { prop1: 1, prop2: 2 }

console.log(obj === fromEntries); // false

6.

猜你喜欢

转载自blog.csdn.net/weixin_42437900/article/details/88108088