WeakRef.prototype.deref()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2021.
deref
方法返回WeakRef
实例的目标对象,如果目标对象已被垃圾收集,则返回undefined
。
语法
obj = ref.deref();
返回值
返回 WeakRef 的目标对象,如果该对象已被垃圾收集,则返回undefined
。
说明
有关一些重要说明,请参阅WeakRef
页面上的Notes on WeakRefs 。
示例
使用 deref
js
const tick = () => {
// Get the element from the weak reference, if it still exists
const element = this.ref.deref();
if (element) {
element.textContent = ++this.count;
} else {
// The element doesn't exist anymore
console.log("The element is gone.");
this.stop();
this.ref = null;
}
};
规范
Specification |
---|
ECMAScript® 2025 Language Specification # sec-weak-ref.prototype.deref |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
deref |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
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.