美文网首页
链判断运算符(?.)

链判断运算符(?.)

作者: coderhzc | 来源:发表于2021-10-24 22:55 被阅读0次
    // 链判断运算符
        let zhangsan = {
          name: "zhangsan",
          girlFriend: {
            name: "lisi",
            boyFriend: {
              name: "wangwu"
            }
          }
        }
    
    // let wangwu = zhangsan.girlFriend.boyFriend.name || "张三"
        let wangwu = (zhangsan && zhangsan.girlFriend && zhangsan.girlFriend.boyFriend && zhangsan.girlFriend.boyFriend.name)
        console.log(wangwu);
    
     // ?.来判断
     let name = zhangsan?.girlFriend?.boyFriend?.name || "张三"
     console.log(name);
    

    实际截图:

    image.png

    相关文章

      网友评论

          本文标题:链判断运算符(?.)

          本文链接:https://www.haomeiwen.com/subject/qqndaltx.html