let a = b.c?.d;
// 编译成
let temp = b.c;
let a = temp === null || temp === undefined ? undefined : temp.d;
// 方法
let result = someInterface?.customMethod?.()
// 注意可选链,不能用于赋值
let object = {};
object?.property = 1; // Uncaught SyntaxError: Invalid left-hand side in assignment
babel 中启用他
// install
npm install --save-dev @babel/plugin-proposal-optional-chaining
// babel config
{
"plugins": [
"@babel/plugin-proposal-optional-chaining" //可选链
"@babel/plugin-proposal-nullish-coalescing-operator", //双问号
]
}