WeakRef.prototype.deref()

WeakRef 实例的 deref() 方法返回此 WeakRef 的目标值,如果目标值已被垃圾收集,则返回 undefined

¥The deref() method of WeakRef instances returns this WeakRef's target value, or undefined if the target value has been garbage-collected.

语法

¥Syntax

js
deref()

参数

¥Parameters

没有任何。

¥None.

返回值

¥Return value

WeakRef 的目标值,可以是对象,也可以是 未注册符号。如果该值已被垃圾回收,则返回 undefined

¥The target value of the WeakRef, which is either an object or a non-registered symbol. Returns undefined if the value has been garbage-collected.

描述

¥Description

请参阅 WeakRef 页的 关于 WeakRef 的注释 部分了解一些重要说明。

¥See the Notes on WeakRefs section of the WeakRef page for some important notes.

示例

¥Examples

使用 deref()

¥Using deref()

有关完整示例,请参阅 WeakRef 页的 示例 部分。

¥See the Examples section of the WeakRef page for the complete example.

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 Language Specification
# sec-weak-ref.prototype.deref

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also