blob: fbabf105e38707bf48adce8f9f12e41e98f94952 (
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
70
71
72
73
74
75
76
77
78
79
80
81
|
---
title: WorkerGlobalScope.self
slug: Web/API/WorkerGlobalScope/self
translation_of: Web/API/WorkerGlobalScope/self
---
<div>{{APIRef("Web Workers API")}}</div>
<p><code><strong>self</strong></code> 是 {{domxref("WorkerGlobalScope")}} 的只读属性,它指向 <code>WorkerGlobalScope</code> 自身。通常情况下,它是如 {{domxref("DedicatedWorkerGlobalScope")}}, {{domxref("SharedWorkerGlobalScope")}}, 或 {{domxref("ServiceWorkerGlobalScope")}} 类型的 scope 。</p>
<h2 id="语法">语法</h2>
<pre class="brush: js">var selfRef = self;</pre>
<h3 id="值">值</h3>
<p>当前 worker 的全局 scope (值取决于你创建的 worker 类型)。</p>
<h2 id="示例">示例</h2>
<p>在 worker 中运行这行代码:</p>
<pre class="brush: js">console.log(self);</pre>
<p>console 中会输出当前 worker 的全局 scope,如下所示:</p>
<pre class="brush: js">DedicatedWorkerGlobalScope {
undefined: undefined, Infinity: Infinity, Math: MathConstructor, NaN: NaN, Intl: Object…}
Infinity: Infinity
Array: function Array() { [native code] }
arguments: null
caller: null
isArray: function isArray() { [native code] }
length: 1
name: "Array"
observe: function observe() { [native code] }
prototype: Array[0]
unobserve: function unobserve() { [native code] }
__proto__: function Empty() {}
<function scope>
ArrayBuffer: function ArrayBuffer() { [native code] }
Blob: function Blob() { [native code] }
Boolean: function Boolean() { [native code] }
DataView: function DataView() { [native code] }
Date: function Date() { [native code] }
DedicatedWorkerGlobalScope: function DedicatedWorkerGlobalScope() { [native code] }
Error: function Error() { [native code] }
<em>// etc. etc.</em>
</pre>
<p>这会列出当前 worker scope 上完整的属性列表,在需要检测某个属性是否可用时非常有用。 详细列表也可查阅 <a href="/zh-CN/docs/Web/API/Worker/Functions_and_classes_available_to_workers">Functions and classes available to Web Workers</a>.</p>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('HTML WHATWG', '#dom-workerglobalscope-self', 'self')}}</td>
<td>{{Spec2('HTML WHATWG')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="兼容性">兼容性</h2>
<p> </p>
<p>{{Compat("api.WorkerGlobalScope.self")}}</p>
<p> </p>
<h2 id="参见">参见</h2>
<p>{{domxref("WorkerGlobalScope")}}</p>
|