对象判断为空不用三目运算处理的快捷方式(可选链 ?.)

// 可选链 ?. 的为空判断

let a, b;
/* 使用前 */ if (!!a) { b.name = 123; } else { b = undefined; } // 使用后 b = a?.name; console.log(b);
 和运算符&&类似

console.log(1 && 2);

 

posted @ 2022-04-20 09:45  Model-Zachary  阅读(33)  评论(0编辑  收藏  举报