链判断运算符(?.)
2021-10-24 本文已影响0人
coderhzc
// 链判断运算符
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);