1. let a = b.c?.d;
    2. // 编译成
    3. let temp = b.c;
    4. let a = temp === null || temp === undefined ? undefined : temp.d;
    5. // 方法
    6. let result = someInterface?.customMethod?.()
    7. // 注意可选链,不能用于赋值
    8. let object = {};
    9. object?.property = 1; // Uncaught SyntaxError: Invalid left-hand side in assignment

    babel 中启用他

    1. // install
    2. npm install --save-dev @babel/plugin-proposal-optional-chaining
    3. // babel config
    4. {
    5. "plugins": [
    6. "@babel/plugin-proposal-optional-chaining" //可选链
    7. "@babel/plugin-proposal-nullish-coalescing-operator", //双问号
    8. ]
    9. }