FinalizationRegistry() 构造函数

FinalizationRegistry() 构造函数创建 FinalizationRegistry 对象。

¥The FinalizationRegistry() constructor creates FinalizationRegistry objects.

语法

¥Syntax

js
new FinalizationRegistry(callbackFn)

注意:FinalizationRegistry() 只能与 new 一起构建。尝试在没有 new 的情况下调用它会抛出 TypeError

¥Note: FinalizationRegistry() can only be constructed with new. Attempting to call it without new throws a TypeError.

参数

¥Parameters

callback

每次垃圾收集注册的目标值时调用的函数。它的返回值被忽略。使用以下参数调用该函数:

heldValue

注册 target 对象时传递给 register() 方法的第二个参数的值。

示例

¥Examples

创建新注册表

¥Creating a new registry

你创建注册表并传入回调:

¥You create the registry passing in the callback:

js
const registry = new FinalizationRegistry((heldValue) => {
  // …
});

规范

Specification
ECMAScript Language Specification
# sec-finalization-registry-constructor

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看