可选链操作符 ?.、逻辑空赋值运算符 ??=、performance、void 操作符

Published: (December 6, 2025 at 04:09 AM EST)
2 min read
Source: Dev.to

Source: Dev.to

可选链操作符 ?.

?. 的语法叫做 可选链操作符(Optional Chaining)。
如果左侧表达式有值就取值;如果是 nullundefined,则立即停止并返回 undefined,不会抛出错误。

逻辑空赋值运算符 ??=

x ??= y 是 JavaScript 中的 逻辑空赋值运算符(Logical Nullish Assignment)。
只有当 xnullundefined 时,才把 y 赋值给 x。如果 x 已经有值(即使是 0false 等),则保持原样,什么都不做。

等价写法

// 写法 A:使用简写
x ??= y;

// 写法 B:原本的逻辑(等价)
if (x === null || x === undefined) {
    x = y;
}

performance

performance 是一个专门用于 精确测量时间 的对象。
它提供比 Date.now() 更高精度(微秒级)的计时能力,主要用于动画、性能监控以及测量时间差。

void 操作符

void 操作符用于对一个表达式求值后 丢弃其返回值,最终返回 undefined

void expression;
// 效果是:执行 expression,返回 undefined。
Back to Blog

Related posts

Read more »