ES12-2021

1. String.prototype.replaceAll()

  • String.prototype.replaceAll() 方法返回一个由替换值(replacement)替换一些或所有匹配的模式(pattern)后的新字符串。模式可以是一个字符串或者一个正则表达式,替换值可以是一个字符串或者一个每次匹配都要调用的回调函数。
const p =
  'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?'

const regex = /dog/gi

console.log(p.replaceAll(regex, 'ferret'))

// expected output: "The quick brown fox jumps over the lazy ferret. If the ferret reacted, was it really lazy?"

2. Promise.any()

  • Promise.any() 方法返回一个带有单一参数的 Promise,该参数是一个迭代器,其中包含一组 Promise 实例。只要迭代器中的任何一个 Promise 实例被解决或拒绝,返回的 Promise 就会被解决或拒绝。
// 1. Promise.any() 方法

3. Logical Assignment Operators

  • 逻辑赋值运算符(&&=, ||=, ??=)是一种新的赋值运算符,它结合了逻辑运算符和赋值运算符的功能。
// 1. &&= 逻辑赋值运算符

// 2. ||= 逻辑赋值运算符

// 3. ??= 逻辑赋值运算符

4. Numeric Separators

  • 数字分隔符(_)是一种新的数字字面量语法,它允许在数字字面量中使用下划线来增强可读性。
// 1. 数字分隔符

const oneMillion = 1_000_000

const creditCard = 1234_5678_9012_3456

const socialSecurity = 999_99_9999

const hexBytes = 0xFF_EC_DE_5E

const bits = 0b1010_0001_1000_0101

const anAmount = 1_000_000_000_000

const inBinary = 0b0101_1111_1001_0000_1000_0110_1110_0111

const withSeparators = 1_000_000_000_000

const justTooBig = 1_000_000_000_000_000

const notANumber = 1_000_000_000_000_000_