javascript.basics icon indicating copy to clipboard operation
javascript.basics copied to clipboard

可选链操作符 - 问号点 (?.)是&&的替代品

Open Kelichao opened this issue 4 years ago • 0 comments

报错情况

var a = {};
console.log(a.b.c)//  Uncaught TypeError: Cannot read property 'c' of undefined

普通解决方式

var a = {};
console.log(a && a.b && a.b.c)// 返回 undefined,不会报错.

// 正确使用方式
consolg.log(a?.b?.c) // 效果同上

Kelichao avatar Jun 24 '21 02:06 Kelichao