ES9-2018
1. rest 参数和扩展运算符
function foo(...args) {
console.log(args)
}
foo(1, 2, 3)
2. 正则表达式命名捕获组
const RE_DATE = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/
const matchObj = RE_DATE.exec('1999-12-31')
const year = matchObj.groups.year
const month = matchObj.groups.month
const day = matchObj.groups.day
3. 正则表达式反向断言
const RE_TWICE = /^(?<word>[a-z]+)(?!\k<word>)$/
RE_TWICE.test('abc')
RE_TWICE.test('abcc')
RE_TWICE.test('abab')
RE_TWICE.test('ababa')
4. 正则表达式 Unicode 转义
;/\u{61}/.test('a') /
𠮷 /
u.test('𠮷')
5. Promise.prototype.finally()
Promise.resolve(123)
.then((res) => {
console.log(res)
})
.finally(() => {
console.log('finally')
})
6. Object.fromEntries()
Object.fromEntries([
['foo', 'bar'],
['baz', 42],
])
for...of 循环 和 await
async function foo() {
for await (const x of createAsyncIterable(['a', 'b'])) {
console.log(x)
}
}
foo()