var test = 1, obj = {}
- test = obj || '嘿嘿'
等价于
if(
obj === '' ||
obj === null ||
obj === undefined ||
obj === 0 ||
obj === false ||
) {
test = '嘿嘿'
} else {
test = obj
}
- test = obj ?? {}
等价于
if (
obj === null ||
obj === undefined ||
) {
test = '嘿嘿'
} else {
test = obj
}
?.可选链接操作符 表达式中的引用如不存在会返回undefined,不会报错
let obj ={}
console.log(obj.a.b)
// TypeError: Cannot read property 'b' of undefined
console.log(obj?.a?.b)
// undefined