var person = {
name:'Licy',
other:{
pro:{
val:'--'
}
},
love:['dance','sing','run'],
say:function(sth) {
console.log(`Hello ${sth}`)
},
}
//访问属性
console.log(person?.name) //Licy
console.log(person?.other?.pro?.val);// --
console.log(person?.age) //undefined
//访问数组
console.log(person?.love?.[1]);//sing
console.log(person?.like?.[1]);//undefined
//访问方法
console.log(person?.say?.('你好'))//"Hello 你好"
console.log(person?.saying?.('你好'))//undefined
- ?? 操作符 过滤 null 和undefined
var person = {
name:'Licy',
other:{
pro:{
val:'--'
}
},
love:['dance','sing','run'],
say:function(sth) {
console.log(`Hello ${sth}`)
},
}
var age = person?.age ?? 18
console.log(age);//18