es10新特性

es10新特性

可选链 ?.

在引用为空(nullish ) (null 或者 undefined) 的情况下不会引起错误,该表达式短路返回值是 undefined

const obj = {
    
    first:{
    
    second:{
    
    third:'老三'}}}
obj.first?.second   // {third:'老三'}
obj.first?.name   // undefined
obj.first?.second?.third // '老三'

空值合并运算符 ??

当左侧的操作数为 null 或者 undefined 时,返回其右侧操作数,否则返回左侧操作数

undefined ?? 'a'   // a
null ?? 'a'   // a
0 ?? 'a'   // 0
true ?? 'a'  // true
false ?? 'a' // false

bigInt(原始类型)

  1. var bigIntNum = 9007199254740993n
  2. var bigIntNum = BigInt(9007199254740);
    ps:尽可能避免通过调用函数 BigInt 方式来实例化超大整型。因为参数的字面量实际也是 Number 类型的一次实例化,超出安全范围的数字,可能会引起精度丢失。

Promise.allSettled([p1,p2,p3…]).then().catch()

des:并发任务中,无论一个任务正常或者异常,都会返回对应的的状态(fulfilled 或者 rejected)与结果(业务value 或者 拒因 reason),在 then 里面通过 filter 来过滤出想要的业务逻辑结果

Promise.allSettled([
  Promise.reject({
    
     code: 500, msg: '服务异常' }),
  Promise.resolve({
    
     code: 200, list: [] }),
  Promise.resolve({
    
     code: 200, list: [] }),
])
  .then((ret) => {
    
    
    console.log('成功');
    console.log(ret);
    // [{status: "rejected", reason: {…}},{status: "fulfilled", value: {…}},{status: "fulfilled", value: {…}}]
  })
  .catch(() => {
    
    
    // 不会执行catch
    console.log('失败');
  });

猜你喜欢

转载自blog.csdn.net/qq_36303110/article/details/110495149