blob: 9d9fdbdba1cdeca721f560455dd2fbf89b74e0ce (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
---
title: WeakRef.prototype.deref()
slug: Web/JavaScript/Reference/Global_Objects/WeakRef/deref
translation_of: Web/JavaScript/Reference/Global_Objects/WeakRef/deref
---
<div>{{JSRef}}</div>
<div></div>
<div><code>deref</code>方法返回{{jsxref("WeakRef")}} 实例的目标对象,如果目标对象已被垃圾收集,则返回<code>undefined</code> 。</div>
<h2 id="语法">语法</h2>
<pre class="syntaxbox notranslate"><code><var>obj</var> = <var>ref</var>.deref();</code>
</pre>
<h3 id="返回值">返回值</h3>
<p>返回WeakRef的目标对象,如果该对象已被垃圾收集,则返回<code>undefined</code> 。</p>
<h2 id="说明">说明</h2>
<p>有关一些重要说明,请参阅{{jsxref("WeakRef")}}页面上的<a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef#Notes_on_WeakRefs">Notes on WeakRefs</a> 。</p>
<h2 id="例子">例子</h2>
<h3 id="使用deref">使用deref</h3>
<p>有关<a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef#Examples">完整示例</a>,请参阅{{jsxref("WeakRef")}}页面的示例部分。</p>
<pre class="brush: js notranslate">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;
}
};</pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">规范</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('WeakRefs', '#sec-weak-ref.prototype.deref', 'WeakRef.prototype.deref()')}}</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{Compat("javascript.builtins.WeakRef.deref")}}</p>
<h2 id="相关链接">相关链接</h2>
<ul>
<li>{{jsxref("WeakRef")}}</li>
</ul>
|