register() method registers an object with a FinalizationRegistry instance so that if the object is garbage-collected, the registry's callback may get called.

句法

registry.register(target, heldValue, [unregisterToken]);
					

参数

target

The target object to register.

heldValue
The value to pass to the finalizer for this object. This cannot be the target 对象。
unregisterToken 可选
A token that may be used with the unregister method later to unregister the target object. If provided (and not undefined ), this must be an object. If not provided, the target cannot be unregistered.

返回值

undefined .

注意事项

Avoid where possible and Notes on cleanup callbacks sections of the FinalizationRegistry page for important caveats.

范例

Using register

The following registers the target object referenced by target , passing in the held value "some value" and passing the target object itself as the unregistration token:

registry.register(target, "some value", target);
					

The following registers the target object referenced by target , passing in another object as the held value, and not passing in any unregistration token (which means target can't be unregistered):

registry.register(target, {"useful": "info about target"});
					

规范

规范
WeakRefs
The definition of 'FinalizationRegistry.prototype.register' in that specification.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
register Chrome 84 Edge 84 Firefox 79 IE No Opera No Safari No WebView Android 84 Chrome Android 84 Firefox Android No Opera Android No Safari iOS No Samsung Internet Android No nodejs 13.0.0 Disabled
13.0.0 Disabled
Disabled From version 13.0.0: this feature is behind the --harmony-weak-refs runtime flag.

图例

完整支持

完整支持

不支持

不支持

用户必须明确启用此特征。

用户必须明确启用此特征。

另请参阅

元数据

  • 最后修改: