deref method returns the WeakRef instance's target object, or undefined if the target object has been garbage-collected.

句法

obj = ref.deref();
					

返回值

The target object of the WeakRef, or undefined if the object has been garbage-collected.

注意事项

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

范例

Using deref

范例 section of the WeakRef page for the complete example.

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;
  }
};
					

规范

规范
WeakRefs
The definition of 'WeakRef.prototype.deref()' in that specification.

浏览器兼容性

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. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
deref Chrome 84 Edge 84 Firefox 79 IE No Opera No Safari No WebView Android 84 Chrome Android 84 Firefox Android No Opera Android No Safari iOS No Samsung Internet Android No nodejs 13.0.0 Disabled
13.0.0 Disabled
Disabled From version 13.0.0: this feature is behind the --harmony-weak-refs runtime flag.

图例

完整支持

完整支持

不支持

不支持

用户必须明确启用此特征。

用户必须明确启用此特征。

另请参阅

元数据

  • 最后修改: