- ES2020
- ES 2019
- 行分隔符(U + 2028)和段分隔符(U + 2029)符号现在允许在字符串文字中,与JSON匹配
- 更加友好的 JSON.stringify
- 新增了Array的
flat()方法和flatMap()方法 - 新增了String的
trimStart()方法和trimEnd()方法 Object.fromEntries()Symbol.prototype.descriptionString.prototype.matchAllFunction.prototype.toString()现在返回精确字符,包括空格和注释- 简化
try{}catch{},修改catch绑定 - 新的基本数据类型
BigInt - globalThis
- import()
- Legacy RegEx
- 私有的实例方法和访问器
ES2020
GlobalThis
console.log(window)console.log(self)console.log(globalThis)
| window | Self | globalThis | |
|---|---|---|---|
| 浏览器 | support | support | support |
| web workers | support | support | |
| Node | support |
Promise.allSettled
const p1 = new Promise((resolve, reject) =>setTimeout(resolve(1), 200))const p2 = new Promise((resolve, reject) =>setTimeout(reject("error"), 300))const p3 = new Promise((resolve, reject) =>setTimeout(resolve(2), 400))// 想要多个并发执行Promise.all([p1, p2, p3]).then().catch()// 问题?,其中一个为reject,则整个会被终止Promise.allSettled([p1,p2,p3]).then().catch()// 三个的结果会依次被打印
Nullish coalescing Operator(空位合并操作符)
const liSi = {basicInfo: {age: 10}}console.log(liSi.basicInfo.age)console.log(liSi.basicInfo.role)// 我们想要给role一个默认值console.log(liSi.basicInfo.role || "student")// 但是,如果role为"",那么输出会出现错误// 解决这个,空位合并操作符console.log(liSi.basicInfo.role ?? "student")
Optinal Chaining Operator(可选链操作符)
const liSi = {}// 会报错,终止代码运行console.log(liSi.basicInfo.age)// 通过逻辑与进行处理console.log(liSi && liSi.basicInfo && liSi.basicInfo.age)// 缺点,潜质校验过多,代码冗余// 使用可选链操作符console.log(liSi?.basicInfo?.age)// 定义BigInt类型数据const big = 123nconst a = 25;const biga = BigInt(a)
BigInt
- JavaScript又一个数字类型Number,number类型的数字有最大值和最小值的限制
const max = Number.MAX_SAFE_INTEGERconsole.log(max)console.log(max + 1)// 已达到了最大值,输出不会变化
ES 2019
行分隔符(U + 2028)和段分隔符(U + 2029)符号现在允许在字符串文字中,与JSON匹配
更加友好的 JSON.stringify
新增了Array的 flat()方法和 flatMap()方法
新增了String的 trimStart()方法和 trimEnd()方法
Object.fromEntries()
Symbol.prototype.description
String.prototype.matchAll
Function.prototype.toString()现在返回精确字符,包括空格和注释
简化 try {} catch {},修改 catch 绑定
新的基本数据类型 BigInt
globalThis
import()
Legacy RegEx
私有的实例方法和访问器
class Persion {// 私有属性#name = "name"// 私有方法#say(){return "hello"}getName(){return this.#name}}const p = new Persion()// p.#name// Private field '#name' must be declared in an enclosing classp.getName()// namep.#say()// Private field '#say' must be declared in an enclosing classclass A{static #CONfig = { env:"test" }showConfig(){return A.#CONfig}}new A().showConfig()// {env: "test"}A.#CONFIG// Private field '#CONFIG' must be declared in an enclosing class
文章
