一、?.用法
let a = {
name:2
}
let b = a?.name
console.log(b)
/**只有a有name这个属性时,才会把name的值赋值给b
* 如果a没有name这个属性,则b为undefined
*/
二、??
let a = undefined //或者null
let c = 1
let b = a??c
console.log(b)
/*
如果a为undefined或者null,则把c的值赋给b
否则把a的值赋给b
*/
output:1
三、??=
let a = 12 //或者null
let c = 1
let b = a??=c
console.log(b)
/*
let b = a??=c:当a的是undefined或者null时,把c的值赋值给a,即相当于let b = c
当a的值非undefined或者null时,即相当于let b = a
*/