此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
WeakRef() 构造函数
Baseline Widely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2021年4月.
* Some parts of this feature may have varying levels of support.
WeakRef() 会创建一个WeakRef 对象,它是对于目标对象的弱引用。
In this article
语法
js
new WeakRef(targetObject)参数
targetObjectWeakRef 要指向的目标对象 (也称作referent)。
示例
>创建一个新的 WeakRef 对象
完整的示例请见WeakRef 主页面。
js
class Counter { constructor(element) { // 创建一个对 DOM 元素的弱引用 this.ref = new WeakRef(element); this.start(); }}规范
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-weak-ref-constructor> |