aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/workerglobalscope
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/workerglobalscope
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/api/workerglobalscope')
-rw-r--r--files/zh-cn/web/api/workerglobalscope/importscripts/index.html83
-rw-r--r--files/zh-cn/web/api/workerglobalscope/index.html286
-rw-r--r--files/zh-cn/web/api/workerglobalscope/self/index.html81
3 files changed, 450 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/workerglobalscope/importscripts/index.html b/files/zh-cn/web/api/workerglobalscope/importscripts/index.html
new file mode 100644
index 0000000000..9a7781a1b8
--- /dev/null
+++ b/files/zh-cn/web/api/workerglobalscope/importscripts/index.html
@@ -0,0 +1,83 @@
+---
+title: WorkerGlobalScope.importScripts()
+slug: Web/API/WorkerGlobalScope/importScripts
+tags:
+ - API
+ - Web Workers
+ - WorkerGlobalScope
+ - Workers
+ - 参考
+ - 方法
+translation_of: Web/API/WorkerGlobalScope/importScripts
+---
+<p>{{APIRef("Web Workers API")}}</p>
+
+<p>{{domxref("WorkerGlobalScope")}} 接口的<code><strong>importScripts()</strong></code> 方法将一个或多个脚本同步导入到工作者的作用域中。</p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="brush: js">self.importScripts('foo.js');
+self.importScripts('foo.js', 'bar.js', ...);</pre>
+
+<h3 id="参数">参数</h3>
+
+<p>{{domxref("DOMString")}} 对象的一个逗号分隔列表,表示要导入的脚本。</p>
+
+<h3 id="返回值">返回值</h3>
+
+<p><em>无。</em></p>
+
+<h3 id="异常">异常</h3>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">异常</th>
+ <th scope="col">描述</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td><code>NetworkError</code></td>
+ <td>要导入的脚本不具有有效的 JavaScript MIME 类型(有效的类型如 <code>text/javascript</code>)。</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="例子">例子</h2>
+
+<p>如果您在一个名为foo.js的单独脚本中编写了一些您想在worker.js中使用的功能,则可以使用以下行导入它:</p>
+
+<pre class="brush: js">importScripts('foo.js');</pre>
+
+<p><code>importScripts()</code> 和 <code>self.importScripts()</code> 实际上是等效的 — 都表示从工作者的内部范围内调用的 <code>importScripts()</code>。</p>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">规范</th>
+ <th scope="col">状态</th>
+ <th scope="col">备注</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('HTML WHATWG', '#dom-workerglobalscope-importscripts', 'importScripts()')}}</td>
+ <td>{{Spec2('HTML WHATWG')}}</td>
+ <td></td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+
+
+<p>{{Compat("api.WorkerGlobalScope.importScripts")}}</p>
+
+<h2 id="也可以看看">也可以看看</h2>
+
+<ul>
+ <li>{{domxref("WorkerGlobalScope")}}</li>
+ <li><a href="/zh-CN/docs/Web/API/Web_Workers_API/Using_web_workers">Using Web workers</a></li>
+</ul>
diff --git a/files/zh-cn/web/api/workerglobalscope/index.html b/files/zh-cn/web/api/workerglobalscope/index.html
new file mode 100644
index 0000000000..8d9fef8dd7
--- /dev/null
+++ b/files/zh-cn/web/api/workerglobalscope/index.html
@@ -0,0 +1,286 @@
+---
+title: WorkerGlobalScope
+slug: Web/API/WorkerGlobalScope
+tags:
+ - API
+ - Interface
+ - NeedsBrowserCompatibility
+ - NeedsTranslation
+ - Reference
+ - TopicStub
+ - WorkerGlobalScope
+ - Workers
+translation_of: Web/API/WorkerGlobalScope
+---
+<p>{{APIRef("Web Workers API")}}</p>
+
+<p> <a href="/en-US/docs/Web/API/Web_Workers_API">Web Workers API</a>  的 <code><strong>WorkerGlobalScope</strong></code> 接口 是一个代表了任何 scope of worker的接口. Workers 没有浏览内容; 这个 scope 包含的信息总是通过 {{domxref("Window")}} objects传递 — 比如 event handlers, the console or the associated {{domxref("WorkerNavigator")}} object.每个 <code>WorkerGlobalScope</code> 都有自己的事件循环.</p>
+
+<p>每个 worker type都有专门的这个接口: {{domxref("DedicatedWorkerGlobalScope")}} for dedicated workers, {{domxref("SharedWorkerGlobalScope")}} for shared workers, and {{domxref("ServiceWorkerGlobalScope")}} for <a href="/en-US/docs/Web/API/ServiceWorker_API">ServiceWorker</a>. <code>self</code> 属性返回每个内容的专门 scope .</p>
+
+<h2 id="Properties">Properties</h2>
+
+<p><em>This interface inherits properties from the {{domxref("EventTarget")}} interface and implements properties from {{domxref("WindowTimers")}}, {{domxref("WindowBase64")}}, and {{domxref("WindowEventHandlers")}}.</em></p>
+
+<h3 id="Standard_properties">Standard properties</h3>
+
+<dl>
+ <dt>{{domxref("WorkerGlobalScope.caches")}} {{readOnlyinline}}</dt>
+ <dd>Returns the {{domxref("CacheStorage")}} object associated with the current context. This object enables <a href="/en-US/docs/Web/API/ServiceWorker_API">service worker</a> functionality such as storing assets for offline use, and generating custom responses to requests.</dd>
+ <dt>{{domxref("WorkerGlobalScope.navigator")}} {{readOnlyinline}}</dt>
+ <dd>Returns the {{domxref("WorkerNavigator")}} associated with the worker. It is a specific navigator object, mostly a subset of the {{domxref("Navigator")}} for browsing scopes, but adapted to workers.</dd>
+ <dt>{{domxref("WorkerGlobalScope.self")}} {{readOnlyinline}}</dt>
+ <dd>Returns a reference to the <code>WorkerGlobalScope</code> itself. Most of the time it is a specific scope like {{domxref("DedicatedWorkerGlobalScope")}},  {{domxref("SharedWorkerGlobalScope")}} or {{domxref("ServiceWorkerGlobalScope")}}.</dd>
+ <dt>{{domxref("WorkerGlobalScope.location")}} {{readOnlyinline}}</dt>
+ <dd>Returns the {{domxref("WorkerLocation")}} associated with the worker. It is a specific location object, mostly a subset of the {{domxref("Location")}} for browsing scopes, but adapted to workers.</dd>
+</dl>
+
+<h3 id="Non-standard_properties">Non-standard properties</h3>
+
+<dl>
+ <dt>{{domxref("WorkerGlobalScope.performance")}} {{readOnlyinline}}</dt>
+ <dd>Returns the {{domxref("Performance")}} associated with the worker. It is a regular performance object, except that only a subset of its property and methods are available to workers.</dd>
+ <dt>{{domxref("WorkerGlobalScope.console")}} {{readOnlyinline}} {{Non-standard_inline}}</dt>
+ <dd>Returns the {{domxref("Console")}} associated with the worker.</dd>
+</dl>
+
+<h3 id="Event_Handlers">Event Handlers</h3>
+
+<p><em>This interface inherits event handlers from the {{domxref("EventTarget")}} interface and implements event handlers from {{domxref("WindowTimers")}}, and {{domxref("WindowBase64")}}.</em></p>
+
+<dl>
+ <dt>{{domxref("WorkerGlobalScope.onerror")}}</dt>
+ <dd>Is an {{domxref("EventHandler")}} representing the code to be called when the {{event("error")}} event is raised.</dd>
+ <dt>{{domxref("WorkerGlobalScope.onoffline")}}</dt>
+ <dd>Is an {{domxref("EventHandler")}} representing the code to be called when the {{event("offline")}} event is raised.</dd>
+ <dt>{{domxref("WorkerGlobalScope.ononline")}}</dt>
+ <dd>Is an {{domxref("EventHandler")}} representing the code to be called when the {{event("online")}} event is raised.</dd>
+ <dt>{{domxref("WorkerGlobalScope.onlanguagechange")}}</dt>
+ <dd>An {{domxref("EventHandler")}} fired at the global/worker scope object when the user's preferred languages change.</dd>
+</dl>
+
+<dl>
+ <dt>{{domxref("WorkerGlobalScope.onclose")}} {{non-standard_inline}}</dt>
+ <dd>Is an {{domxref("EventHandler")}} representing the code to be called when the {{event("close")}} event is raised.</dd>
+ <dt>{{domxref("WorkerGlobalScope.onrejectionhandled")}} {{non-standard_inline}}</dt>
+ <dd>An event handler for handled <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" title="The Promise object is used for deferred and asynchronous computations. A Promise represents an operation that hasn't completed yet, but is expected in the future."><code>Promise</code></a> rejection events.</dd>
+ <dt>{{domxref("WorkerGlobalScope.onunhandledrejection")}} {{non-standard_inline}}</dt>
+ <dd>An event handler for unhandled <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" title="The Promise object is used for deferred and asynchronous computations. A Promise represents an operation that hasn't completed yet, but is expected in the future."><code>Promise</code></a> rejection events.</dd>
+</dl>
+
+<h2 id="Methods">Methods</h2>
+
+<p><em>This interface inherits methods from the {{domxref("EventTarget")}} interface and implements methods from {{domxref("WindowTimers")}}, {{domxref("WindowBase64")}}, {{domxref("WindowEventHandlers")}}, and {{domxref("GlobalFetch")}}.</em></p>
+
+<h3 id="Standard_methods">Standard methods</h3>
+
+<dl>
+ <dt>{{domxref("WorkerGlobalScope.close()")}}</dt>
+ <dd>Discards any tasks queued in the <code>WorkerGlobalScope</code>'s event loop, effectively closing this particular scope.</dd>
+ <dt>{{domxref("WorkerGlobalScope.importScripts()")}}</dt>
+ <dd>Imports one or more scripts into the worker's scope. You can specify as many as you'd like, separated by commas. For example:<code> importScripts('foo.js', 'bar.js');</code></dd>
+</dl>
+
+<h3 id="Non-standard_methods">Non-standard methods</h3>
+
+<dl>
+ <dt>{{domxref("WorkerGlobalScope.dump()")}} {{non-standard_inline}}</dt>
+ <dd>Allows you to write a message to stdout — i.e. in your terminal. This is the same as Firefox's {{domxref("window.dump")}}, but for workers.</dd>
+</dl>
+
+<h3 id="Methods_implemented_from_elsewhere">Methods implemented from elsewhere</h3>
+
+<dl>
+ <dt>{{domxref("WindowBase64.atob()")}}</dt>
+ <dd>Decodes a string of data which has been encoded using base-64 encoding.</dd>
+ <dt>{{domxref("WindowBase64.btoa()")}}</dt>
+ <dd>Creates a base-64 encoded ASCII string from a string of binary data.</dd>
+ <dt>{{domxref("WindowTimers.clearInterval()")}}</dt>
+ <dd>Cancels the repeated execution set using {{domxref("WindowTimers.setInterval()")}}.</dd>
+ <dt>{{domxref("WindowTimers.clearTimeout()")}}</dt>
+ <dd>Cancels the repeated execution set using {{domxref("WindowTimers.setTimeout()")}}.</dd>
+ <dt>{{domxref("ImageBitmapFactories.createImageBitmap()")}}</dt>
+ <dd>Accepts a variety of different image sources, and returns a {{domxref("Promise")}} which resolves to an {{domxref("ImageBitmap")}}.</dd>
+ <dt>{{domxref("GlobalFetch.fetch()")}}</dt>
+ <dd>Starts the process of fetching a resource.</dd>
+ <dt>{{domxref("WindowTimers.setInterval()")}}</dt>
+ <dd>Schedules the execution of a function each X milliseconds.</dd>
+ <dt>{{domxref("WindowTimers.setTimeout()")}}</dt>
+ <dd>Sets a delay for executing a function.</dd>
+</dl>
+
+<h2 id="Example">Example</h2>
+
+<p>You won't access <code>WorkerGlobalScope</code> directly in your code; however, its properties and methods are inherited by more specific global scopes such as {{domxref("DedicatedWorkerGlobalScope")}} and {{domxref("SharedWorkerGlobalScope")}}. For example, you could import another script into the worker and print out the contents of the worker scope's <code>navigator</code> object using the following two lines:</p>
+
+<pre class="brush: js">importScripts('foo.js');
+console.log(navigator);</pre>
+
+<div class="note">
+<p><strong>Note</strong>: Since the global scope of the worker script is effectively the global scope of the worker you are running ({{domxref("DedicatedWorkerGlobalScope")}} or whatever) and all worker global scopes inherit methods, properties, etc. from <code>WorkerGlobalScope</code>, you can run lines such as those above without specifying a parent object.</p>
+</div>
+
+<h2 id="Specifications">Specifications</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', '#workerglobalscope', 'WorkerGlobalScope')}}</td>
+ <td>{{Spec2('HTML WHATWG')}}</td>
+ <td>No change from {{SpecName("Web Workers")}}.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('Service Workers')}}</td>
+ <td>{{Spec2('Service Workers')}}</td>
+ <td>Defines <code>caches</code>.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('Web Workers', '#workerglobalscope', 'WorkerGlobalScope')}}</td>
+ <td>{{Spec2('Web Workers')}}</td>
+ <td>Initial definition</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility">Browser compatibility</h2>
+
+<p>{{CompatibilityTable}}</p>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Chrome</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{CompatChrome(4)}}</td>
+ <td>{{CompatGeckoDesktop("1.9.1")}}</td>
+ <td>10</td>
+ <td>10.6</td>
+ <td>4</td>
+ </tr>
+ <tr>
+ <td><code>ononline</code>, <code>onoffline</code></td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatGeckoDesktop("29")}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatUnknown}}</td>
+ </tr>
+ <tr>
+ <td><code>console</code> {{Non-standard_inline}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatGeckoDesktop("29")}}<sup>[1]</sup><br>
+ {{CompatGeckoDesktop("30")}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatUnknown}}</td>
+ </tr>
+ <tr>
+ <td><code>performance</code></td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatGeckoDesktop("34")}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatUnknown}}</td>
+ </tr>
+ <tr>
+ <td><code>caches</code></td>
+ <td>{{CompatChrome(40)}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatNo}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Android</th>
+ <th>Chrome for Android</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>Firefox OS (Gecko)</th>
+ <th>IE Mobile</th>
+ <th>Opera Mobile</th>
+ <th>Safari Mobile</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatGeckoMobile("1.9.1")}}</td>
+ <td>1.0.1</td>
+ <td>10</td>
+ <td>11.5</td>
+ <td>5.1</td>
+ </tr>
+ <tr>
+ <td><code>ononline</code>, <code>onoffline</code></td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatGeckoMobile("29")}}<sup>[1]</sup><br>
+ {{CompatGeckoMobile("30")}}</td>
+ <td>1.4</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatUnknown}}</td>
+ </tr>
+ <tr>
+ <td><code>console</code> {{Non-standard_inline}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatGeckoMobile("29")}}</td>
+ <td>1.4</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatUnknown}}</td>
+ </tr>
+ <tr>
+ <td><code>performance</code></td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatGeckoMobile("34")}}</td>
+ <td>2.1</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatUnknown}}</td>
+ </tr>
+ <tr>
+ <td><code>caches</code></td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<p>[1] Gecko 29 implemented this as <code>WorkerConsole</code>. Since version 30 it uses the regular <code>Console</code>.</p>
+
+<h2 id="See_also">See also</h2>
+
+<ul>
+ <li>Other global object interface: {{domxref("Window")}}, {{domxref("DedicatedWorkerGlobalScope")}}, {{domxref("SharedWorkerGlobalScope")}}, , {{domxref("ServiceWorkerGlobalScope")}}</li>
+ <li>Other Worker-related interfaces: {{domxref("Worker")}}, {{domxref("WorkerLocation")}}, {{domxref("WorkerGlobalScope")}}, and {{domxref("ServiceWorkerGlobalScope")}}.</li>
+ <li><a href="/en-US/docs/Web/Guide/Performance/Using_web_workers">Using web workers.</a></li>
+</ul>
diff --git a/files/zh-cn/web/api/workerglobalscope/self/index.html b/files/zh-cn/web/api/workerglobalscope/self/index.html
new file mode 100644
index 0000000000..fbabf105e3
--- /dev/null
+++ b/files/zh-cn/web/api/workerglobalscope/self/index.html
@@ -0,0 +1,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() {}
+ &lt;function scope&gt;
+    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>