Skip to content

ES10

版本记录

版本介绍

来源: https://262.ecma-international.org/10.0/ Introduction

This specification, the 10th edition, introduces a few new built-in functions: flat and flatMap on Array.prototype for flattening arrays, Object.fromEntries for directly turning the return value of Object.entries into a new Object, and trimStart and trimEnd on String.prototype as better-named alternatives to the widely implemented but non-standard String.prototype.trimLeft and trimRight built-ins. In addition, this specification includes a few minor updates to syntax and semantics. Updated syntax includes optional catch binding parameters and allowing U+2028 (LINE SEPARATOR) and U+2029 (PARAGRAPH SEPARATOR) in string literals to align with JSON. Other updates include requiring that Array.prototype.sort be a stable sort, requiring that JSON.stringify return well-formed UTF-8 regardless of input, and clarifying Function.prototype.toString by requiring that it either return the corresponding original source text or a standard placeholder.

版本概要

版本特性

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

String.prototype.trim()

Stringtrim() 方法会从字符串的两端移除空白字符,并返回一个新的字符串,而不会修改原始字符串。

要返回一个仅从一端修剪空白字符的新字符串,请使用 trimStart()trimEnd()

js
const greeting = '   Hello world!   '

console.log(greeting)
// Expected output: "   Hello world!   ";

console.log(greeting.trim())
// Expected output: "Hello world!";

String.prototype.trimStart()

StringtrimStart() 方法会从字符串的开头移除空白字符,并返回一个新的字符串,而不会修改原始字符串。trimLeft() 是该方法的别名。

js
const greeting = '   Hello world!   '

console.log(greeting)
// Expected output: "   Hello world!   ";

console.log(greeting.trimStart())
// Expected output: "Hello world!   ";

String.prototype.trimEnd()

StringtrimEnd() 方法会从字符串的结尾移除空白字符,并返回一个新的字符串,而不会修改原始字符串。trimRight() 是该方法的别名。

js
const greeting = '   Hello world!   '

console.log(greeting)
// Expected output: "   Hello world!   ";

console.log(greeting.trimEnd())
// Expected output: "   Hello world!";

Array.prototype.flat()/Array.prototype.flatMap()

Array.prototype.flat()

flat() 方法创建一个新的数组,并根据指定深度递归地将所有子数组元素拼接到新的数组中。

js
const arr1 = [0, 1, 2, [3, 4]]

console.log(arr1.flat())
// expected output: Array [0, 1, 2, 3, 4]

const arr2 = [0, 1, [2, [3, [4, 5]]]]

console.log(arr2.flat())
// expected output: Array [0, 1, 2, Array [3, Array [4, 5]]]

console.log(arr2.flat(2))
// expected output: Array [0, 1, 2, 3, Array [4, 5]]

console.log(arr2.flat(Infinity))
// expected output: Array [0, 1, 2, 3, 4, 5]

Array.prototype.flatMap()

flatMap() 方法对数组中的每个元素应用给定的回调函数,然后将结果展开一级,返回一个新数组。它等价于在调用 map() 方法后再调用深度为 1 的 flat() 方法(arr.map(...args).flat()),但比分别调用这两个方法稍微更高效一些。

js
const arr1 = [1, 2, 1]

const result = arr1.flatMap((num) => (num === 2 ? [2, 2] : 1))

console.log(result)
// Expected output: Array [1, 2, 2, 1]

Object.fromEntries()

Object.fromEntries() 静态方法将键值对列表转换为一个对象。

js
const entries = new Map([
  ['foo', 'bar'],
  ['baz', 42],
])

const obj = Object.fromEntries(entries)

console.log(obj)
// Expected output: Object { foo: "bar", baz: 42 }

Function.prototype.toString()

Function 实例的 toString() 方法返回一个表示该函数源码的字符串。

js
function sum(a, b) {
  return a + b
}

console.log(sum.toString())
// Expected output: "function sum(a, b) {
//                     return a + b;
//                   }"

console.log(Math.abs.toString())
// Expected output: "function abs() { [native code] }"

Symbol.prototype.description

description 是一个只读属性,它会返回 Symbol 对象的可选描述的字符串。

js
console.log(Symbol('desc').description)
// Expected output: "desc"

console.log(Symbol.iterator.description)
// Expected output: "Symbol.iterator"

console.log(Symbol.for('foo').description)
// Expected output: "foo"

console.log(`${Symbol('foo').description}bar`)
// Expected output: "foobar"

JSON superset

TODO: 待补充

JSON.stringify()

js
console.log(JSON.stringify({ x: 5, y: 6 }))
// Expected output: '{"x":5,"y":6}'

console.log(
  JSON.stringify([new Number(3), new String('false'), new Boolean(false)])
)
// Expected output: '[3,"false",false]'

console.log(JSON.stringify({ x: [10, undefined, function () {}, Symbol('')] }))
// Expected output: '{"x":[10,null,null,null]}'

console.log(JSON.stringify(new Date(2006, 0, 2, 15, 4, 5)))
// Expected output: '"2006-01-02T15:04:05.000Z"'

可选的 catch 绑定

异常绑定是可写的。例如,你可能需要规范异常值,以确保它是一个 Error 对象。

js
try {
  throw '哦豁;这不是一个 Error 对象'
} catch (e) {
  if (!(e instanceof Error)) {
    e = new Error(e)
  }
  console.error(e.message)
}

如果你不需要异常值,你可以省略异常变量及其周围的括号。

js
function isValidJSON(text) {
  try {
    JSON.parse(text)
    return true
  } catch {
    return false
  }
}