跳到主要内容

ES10 (ES2019)

· 阅读需 6 分钟

ES2019(ES10)增强了数组、字符串、对象等常用操作的简洁性和功能性,同时对 JSON 处理和 Symbol 描述的访问做了增强。这些更新使 JavaScript 代码更加简洁、易读,并提供了更好的开发体验。

ES2019(ES10)增强了数组、字符串、对象等常用操作的简洁性和功能性,同时对 JSON 处理和 Symbol 描述的访问做了增强。

  1. Array.prototype.flat( )
  2. Array.prototype.flatMap( )
  3. String.prototype.trimStart( ) / trimEnd( )
  4. Optional catch Binding
  5. Object.fromEntries( )
  6. Symbol.prototype.description
  7. Function.prototype.toString( ) Revision
  8. Well-formed JSON.stringify( )

1. Array.prototype.flat( )

flat( ) 方法用于将嵌套的数组"拍平",即将多维数组展开拍平。你可以通过传递一个参数来指定拉平的深度,默认为 1。

const arr = [1, [2, [3, [4]], 5]];
console.log(arr.flat());
console.log(arr.flat(2));
console.log(arr.flat(Infinity)); // 无限展开,最终变成一维数组

2. Array.prototype.flatMap( )

flatMap( ) 是 map( ) 和 flat( ) 的结合体。它首先对数组的每个元素执行一个映射函数,然后将结果"拍平"一层。

let arr = [1, 2, 3, 4, 5];
// arr = arr.map((x) => [x, x * 2, x * 3]);
// console.log(arr.flat());

arr = arr.flatMap((x) => [x, x * 2, x * 3]);
console.log(arr);

优点:相比于 map( ).flat( ) 组合,flatMap( ) 性能更高,语法也更简洁。

缺点:无法指定拍平的层数

arr = arr.flatMap((x) => [x, [x * 2], x * 3]);
console.log(arr);
/*
[
1, [ 2 ], 3,
2, [ 4 ], 6,
3, [ 6 ], 9,
4, [ 8 ], 12,
5, [ 10 ], 15
]
*/

像上面的例子,因为无法指定拍平的层数,所以最终得到的结果仍然包含嵌套的数组。这种情况只能使用 map( ).flat( ) 组合。

arr = arr.map((x) => [x, [x * 2], x * 3]);
console.log(arr.flat(Infinity));

3. String.prototype.trimStart( ) / trimEnd( )

trimStart( ) 和 trimEnd( ) 方法分别用于去除字符串开头和结尾的空白字符。之前只有 trim( ) 方法,它会去除字符串两端的空白字符。

const str = "   Hello World   ";
console.log(str.trimStart());
console.log(str.trimEnd());

4. Optional catch Binding

在 ES2019 之前,catch 块必须要定义一个错误参数,即使你不打算使用它。ES2019 允许在 catch 块中省略对错误参数的声明。

try {
} catch {
console.log("捕获到异常");
}

5. Object.fromEntries( )

Object.fromEntries( ) 方法是 Object.entries( ) 的反转。它将一个键值对数组转换为对象。这对处理 Map 等结构非常有用。

const arr = [
["name", "zhangsan"],
["age", 20],
];
console.log(Object.fromEntries(arr));

应用场景:常见的场景是将 Map 转换为对象

const m = new Map([
["name", "张三"],
["age", 20],
["gender", "男"],
]);
console.log(Object.fromEntries(m));

6. Symbol.prototype.description

在 ES2019 之前,Symbol 类型没有一个直接获取其描述的简单方法。ES2019 为 Symbol 添加了 description 属性,允许你访问 Symbol 创建时传递的描述信息。

const sym = Symbol("这是一个简单的Symbol");
console.log(sym.description);

7. Function.prototype.toString( ) Revision

ES2019 对 Function.prototype.toString( ) 方法进行了修订,规定该方法必须返回函数的准确源代码,包括注释、空格和格式化。在此之前,不同 JavaScript 引擎对 toString( ) 的行为可能存在差异,部分引擎在返回函数源代码时会移除注释或修改格式。

ES2019 之前:不同引擎可能有不同的行为

function example() {
// 这是一个注释
return "Hello, world!";
}

// 在某些引擎中,toString() 可能会移除注释
console.log(example.toString());
// 输出可能是:
// "function example() { return 'Hello, world!'; }" (注释和空格被移除)

ES2019 之后:修订了 Function.prototype.toString(),要求它返回函数的准确源代码,包括注释、空白字符和格式。

function example() {
// 这是一个注释
return "Hello, world!";
}

console.log(example.toString());
/*
function example() {
// 这是一个注释
return "Hello, world!";
}
*/

总的来说,这一修订确保了 Function.prototype.toString( ) 在所有环境中的一致性,返回的字符串就是函数的 真实原始源代码,这在调试和代码分析时非常有帮助。

8. Well-formed JSON.stringify( )

先简单回顾一下 JSON.stringify( ) 方法:

const obj = { name: "Alice", age: 25 };
// 将一个 JSON 对象转为 JSON 字符串
const jsonString = JSON.stringify(obj);
console.log(jsonString);
// '{"name":"Alice","age":25}' -> This is a string in JSON format.

在 ES2019 之前,JSON.stringify( ) 处理一些特殊的 Unicode 字符(如 U+2028 和 U+2029,即段落分隔符和行分隔符)时不会对其进行转义,这会导致生成的 JSON 无法通过解析器解析。

Well-formed JSON.stringify( ) 是 ES2019 中的一个改进,修复了 JSON.stringify( ) 在处理某些特定 Unicode 字符时生成无效 JSON 的问题。

例如,从 ES2019 开始,JSON.stringify( ) 会对 U+2028 和 U+2029 等特殊字符进行转义,确保生成的 JSON 是有效的。

const str = "Hello\u2028World"; // 包含特殊字符的字符串
const jsonstr = JSON.stringify(str); // 转为 JSON 字符串
console.log(jsonstr); // "Hello World"

-EOF-