js嵌套对象报错Uncaught TypeError: Cannot read property 'name' of undefined at解决方法

const user = { name:{ age:12 } }

我们打印

console.log(user.name1.age)

会报错Uncaught TypeError: Cannot read property 'name1' of undefined     at

这时:console.log(((user||{}).name1||{}).age)就不会报错,打印出undefined

这种对于嵌套对象比较适合,但数组不是很好

具体可参考:https://www.javascriptc.com/3457.html

原文地址:https://www.cnblogs.com/yck123/p/12322106.html