1.可选链操作符(Optional Chaining)
stack overflow上看到这样子一个问题
臭名昭著的错误Uncaught TypeError: Cannot read property...
你就需要像上面那种解决方案,依次深入判断是否存在,用&&进行链接。
要确定之前的表达式的值非null和非undefined。
但是你如果使用可选链式调用,便捷很多:
let IT = response[0]?.title?;
2.空位合并操作符,用 ?? 表示
如果表达式在??的左侧运算符求值为 undefined 或 null,就返回其右侧默认值。
栗子:适用于 如果a.b属性值为"",你使用默认赋值判断了, a.b = a.b || 18; 你的本意是希望如果a没有b属性,就默认赋值为18,可现在a有b属性,但其值为空字符串,却依旧赋值为18.
3.:Promise.allSettled
我们需要一种机制,如果并发任务中,无论一个任务正常或者异常,都会返回对应的的状态(fulfilled
或者 rejected
)与结果(业务 value
或者 拒因 reason
),在 then
里面通过 filter
来过滤出想要的业务逻辑结果,这就能最大限度的保障业务当前状态的可访问性,而 Promise.allSettled
就是解决这问题的。
4.