--- title: WeakRef.prototype.deref() slug: Web/JavaScript/Reference/Global_Objects/WeakRef/deref translation_of: Web/JavaScript/Reference/Global_Objects/WeakRef/deref ---
deref方法返回{{jsxref("WeakRef")}} 实例的目标对象,如果目标对象已被垃圾收集,则返回undefined 。obj = ref.deref();
返回WeakRef的目标对象,如果该对象已被垃圾收集,则返回undefined 。
有关一些重要说明,请参阅{{jsxref("WeakRef")}}页面上的Notes on WeakRefs 。
有关完整示例,请参阅{{jsxref("WeakRef")}}页面的示例部分。
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;
}
};
| 规范 |
|---|
| {{SpecName('WeakRefs', '#sec-weak-ref.prototype.deref', 'WeakRef.prototype.deref()')}} |
{{Compat("javascript.builtins.WeakRef.deref")}}