为什么读取null的属性有时是未定义的,但其他时候会抛出错误



示例1:当resnull时,coordsundefined

const res = await Geolocation.getGeolocation();
const { coords } = res || {};

示例2:当resnull时,我得到TypeError: Cannot read properties of null (reading 'points')

const res = await API.getPoints(
latitude,
longitude,
market,
);
const { points } = res;

在我的代码中,示例2紧跟在示例1之后,为什么行为会有所不同?

之间的差异

const { coords } = res || {};

const { points } = res;

是CCD_ 8部分。如果res是falsy,则它使表达式的整个右侧等于{},因此允许执行{coords} = {}

相关内容

最新更新