可选链操作符( ?. )允许读取位于连接对象链深处的属性的值,而不必明确验证链中的每个引用是否有效。?. 操作符的功能类似于 . 链式操作符,不同之处在于,在引用为空(nullish ) (null 或者 undefined) 的情况下不会引起错误,该表达式短路返回值是 undefined。与函数调用一起使用时,如果给定的函数不存在,则返回 undefined。
    空值合并操作符??)是一个逻辑操作符,当左侧的操作数为 null 或者 undefined 时,返回其右侧操作数,否则返回左侧操作数。

    1. const adventurer = {
    2. name: 'Alice',
    3. cat: {
    4. name: 'Dinah'
    5. }
    6. };
    7. const dogName = adventurer.dog?.name ?? "暗之城";
    8. console.log(dogName); // “暗之城”
    9. console.log(adventurer.someNonExistentMethod?.());
    10. // expected output: undefined