ES 版本新特性

247 阅读1分钟

ES2020(es11)

可选链

查询多层次的对象时,不需要进行多次校验,例如查询a.b.c.d

a && a.b && a.b.c && a.b.c.d = a?.b?.c?.d

空位合并操作符

左侧运算符只有是undefined 或者 null的情况下 才会返回右侧的值

let a = 0;
a || 100; // 100
a ?? 100; // 0

promise.allSetteld

弥补promise.all 存在某个promise是reject情况下只能返回reject值的情况

Promise.all([Promise.resolve(1),Promise.reject(2)]).catch(rejected=>console.log(rejected)) 
// 2
Promise.all([Promise.resolve(1),Promise.reject(2)]).then(res=>console.log(res)) 
// [{status: "fulfilled", value: 1},{status: "rejected", reason: 2}]

Dymadic import

import('/module/') 返回一个promise对象 跟import * from 'module' 有区别。 动态导入某块,提高应用程序性能(例如首屏渲染的时候,按钮点击事件中使用按需加在,可以减少首次加载时资源的请求)


globalThis

统一了个端的全局对象 eg:在浏览器中globalThis === window,在node环境中 globalThis === global


BigInt

js基本数据类型又添加一个成员 包括:(undefined,null,string,number,boolean,symbol,bigint)


ES2019(es10)