可选链操作符 ?.、逻辑空赋值运算符 ??=、performance、void 操作符
Source: Dev.to
可选链操作符 ?.
?. 的语法叫做 可选链操作符(Optional Chaining)。
如果左侧表达式有值就取值;如果是 null 或 undefined,则立即停止并返回 undefined,不会抛出错误。
逻辑空赋值运算符 ??=
x ??= y 是 JavaScript 中的 逻辑空赋值运算符(Logical Nullish Assignment)。
只有当 x 为 null 或 undefined 时,才把 y 赋值给 x。如果 x 已经有值(即使是 0、false 等),则保持原样,什么都不做。
等价写法
// 写法 A:使用简写
x ??= y;
// 写法 B:原本的逻辑(等价)
if (x === null || x === undefined) {
x = y;
}
performance
performance 是一个专门用于 精确测量时间 的对象。
它提供比 Date.now() 更高精度(微秒级)的计时能力,主要用于动画、性能监控以及测量时间差。
void 操作符
void 操作符用于对一个表达式求值后 丢弃其返回值,最终返回 undefined。
void expression;
// 效果是:执行 expression,返回 undefined。