# ECMAScript
# 2021
String.prototype.replaceAll
: 展示 'hello world'.replaceAll('l', 'L') // 'heLLo worLd'Promise.any
: 返回第一个获得的promise
或AggregateError
try {
const first = await Promise.any(promises);
// Any of the promises was fulfilled.
} catch (error) {
// All of the promises were rejected.
}
1
2
3
4
5
6
2
3
4
5
6
名称 | 描述 | 备注 |
---|---|---|
Promise.allSettled | 不短路 | 在 ES2020 中添加 ✅ |
Promise.all | 输入值被拒绝时发生短路 | 在 ES2015 中添加 ✅ |
Promise.race | 输入值稳定后发生短路 | 在 ES2015 中添加 ✅ |
Promise.any | 满足输入值时发生短路 | 这个建议 🆕 预定于 ES2021 |
- WeakRefs (opens new window): 弱引用,不会阻止垃圾回收进行
- Logical Assignment Operators (opens new window): 结合逻辑运算符和赋值表达式
a ||= b
a || (a = b)
a &&= b
a && (a = b)
a ??= b
a ?? (a = b)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
Numeric separators
:1_000_000_000
,1_123_123_321.38
# 2020
String.prototype.matchAll
: 结合Array.from
使用- import(): 动态导入
- BigInt:
9213817878187189378912737189738912n
- Promise.allSettled (opens new window): 不在乎成功或失败
const promises = [fetch('index.html'), fetch('https://does-not-exist/')]
const results = await Promise.allSettled(promises);
const errors = results
.filter(p => p.status === 'rejected') // 收集错误的回调
.map(p => p.reason);
1
2
3
4
5
6
2
3
4
5
6
- globalThis (opens new window): 未理解
- for-in mechanics:
- Optional Chaining: 可选链,
a?.b?.c?.d
,a?.b?.()
- Nullish coalescing Operator: a = b ?? 'default string' // b 为
null
orundefined
时有效,对空字符串
和0
不作处理 import.meta
(opens new window): 未理解,未实践
← 三次握手和四次挥手 TypeScript →