我不确定我是否了解可选链接的 js 实现背后的逻辑。
const a = {b:1}
1 > console.log(a?.c) => undefined
2 > console.log(a?.c?.d) => undefined
3 > console.log(a?.c.d) => Uncaught TypeError: Cannot read property 'd' of undefined
一切都说得过去。然后:
4 > console.log(a?.c?.d.e) => undefined
5 > console.log(a?.c?.d.e.f.g) => undefined
访问 undefined 的属性会引发错误(#3),但在 2 个可选链接之后访问任意数量的不存在的嵌套属性不会再引发错误。