WeakRef.prototype.deref()
语法
obj = ref.deref();
返回值
返回WeakRef的目标对象,如果该对象已被垃圾收集,则返回undefined
。
说明
有关一些重要说明,请参阅WeakRef
页面上的Notes on WeakRefs 。
例子
使用deref
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;
}
};
规范
浏览器兼容性
BCD tables only load in the browser