aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/worker
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/worker
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/worker')
-rw-r--r--files/zh-cn/web/api/worker/index.html106
-rw-r--r--files/zh-cn/web/api/worker/message_event/index.html85
-rw-r--r--files/zh-cn/web/api/worker/messageerror_event/index.html87
-rw-r--r--files/zh-cn/web/api/worker/onmessage/index.html124
-rw-r--r--files/zh-cn/web/api/worker/onmessageerror/index.html43
-rw-r--r--files/zh-cn/web/api/worker/postmessage/index.html210
-rw-r--r--files/zh-cn/web/api/worker/terminate/index.html106
-rw-r--r--files/zh-cn/web/api/worker/worker/index.html112
8 files changed, 873 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/worker/index.html b/files/zh-cn/web/api/worker/index.html
new file mode 100644
index 0000000000..915aceb681
--- /dev/null
+++ b/files/zh-cn/web/api/worker/index.html
@@ -0,0 +1,106 @@
+---
+title: Worker
+slug: Web/API/Worker
+tags:
+ - DOM
+ - Worker
+translation_of: Web/API/Worker
+---
+<p>{{APIRef("Web Workers API")}}</p>
+
+<p>Worker 接口是 <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API">Web Workers API </a>的一部分,指的是一种可由脚本创建的后台任务,任务执行中可以向其创建者收发信息。要创建一个 Worker<strong> ,</strong>只须调用 <code>Worker(URL) </code>构造函数,函数参数 `URL` 为指定的脚本。</p>
+
+<p>Worker 也可以创建新的 Worker,当然,所有 Worker 必须与其创建者<a href="https://wiki.developer.mozilla.org/zh-CN/docs/Web/Security/Same-origin_policy">同源</a>(注意:<a href="https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/5R3B4RN4GHU">Blink</a>暂时不支持嵌套 Worker)。 </p>
+
+<p>需要注意的是,不是所有函数和构造函数(或者说…类)都可以在 Worker 中使用。具体参考页面 <a href="https://developer.mozilla.org/zh-CN/docs/Web/API/Web_Workers_API/Functions_and_classes_available_to_workers">Worker 所支持的函数和类</a>。Worker 可以使用 <a class="internal" href="/en/DOM/XMLHttpRequest" title="En/XMLHttpRequest"><code>XMLHttpRequest</code></a> 发送请求,但是请求的<span style="line-height: inherit;">  </span><code style="font-size: 14px; line-height: inherit;">responseXML</code><span style="line-height: inherit;"> 与 </span><code style="font-size: 14px; line-height: inherit;">channel</code><span style="line-height: inherit;"> 两个属性值始终返回</span><span style="line-height: inherit;"> </span><code style="font-size: 14px; line-height: inherit;">null</code> (<code>fetch</code> 仍可正常使用,没有类似的限制)。 </p>
+
+<div class="blockIndicator note">
+<p> 如果你要在火狐浏览器的扩展使用 Worker 访问 <a href="https://wiki.developer.mozilla.org/en-US/docs/Mozilla/js-ctypes">js-ctypes</a>,应使用 {{ domxref("ChromeWorker") }} 对象来替代。(译者注:这里没有看懂,希望有人能驳正,或添加说明)</p>
+</div>
+
+<h2 id="构造函数">构造函数</h2>
+
+<dl>
+ <dt>{{domxref("Worker.Worker", "Worker()")}}</dt>
+ <dd>创建一个专用Web worker,它只执行URL指定的脚本。使用 <a href="https://wiki.developer.mozilla.org/en-US/docs/Web/API/Blob">Blob URL</a> 作为参数亦可。</dd>
+</dl>
+
+<h2 id="属性">属性</h2>
+
+<p><em>继承</em>父对象<em>{{domxref("EventTarget")}} 的属性,以及实现对象 {{domxref("AbstractWorker")}}的属性。</em></p>
+
+<h3 id="事件句柄"><em>事件句柄</em></h3>
+
+<dl>
+ <dt>{{domxref("AbstractWorker.onerror")}}</dt>
+ <dd>当{{domxref("ErrorEvent")}} 类型的事件冒泡到 worker 时,事件监听函数 {{ domxref("EventListener") }} 被调用. 它继承于 {{domxref("AbstractWorker")}}.</dd>
+ <dt>{{domxref("Worker.onmessage")}}</dt>
+ <dd>当{{domxref("MessageEvent")}}类型的事件冒泡到 worker 时,事件监听函数 {{ domxref("EventListener") }} 被调用.  例如,一个消息通过 {{domxref("DedicatedWorkerGlobalScope.postMessage")}},从执行者发送到父页面对象,消息保存在事件对象的 {{domxref("MessageEvent.data", "data")}} 属性中.</dd>
+ <dt>{{domxref("Worker.onmessageerror")}}</dt>
+ <dd>当{{event("messageerror")}} 类型的事件发生时,对应的{{domxref("EventHandler")}} 代码被调用。</dd>
+</dl>
+
+<h2 id="方法">方法</h2>
+
+<p><em>继承</em>父对象<em>{{domxref("EventTarget")}} 的方法,以及实现对象 {{domxref("AbstractWorker")}}的方法。</em></p>
+
+<dl>
+ <dt>{{domxref("Worker.postMessage()")}}</dt>
+ <dd>发送一条消息到最近的外层对象,消息可由任何 JavaScript 对象组成。</dd>
+</dl>
+
+<dl>
+ <dt>{{domxref("Worker.terminate()")}}</dt>
+ <dd>立即终止 worker。该方法不会给 worker 留下任何完成操作的机会;就是简单的立即停止。Service Woker 不支持这个方法。</dd>
+</dl>
+
+<h2 id="示例">示例</h2>
+
+<p>下面的代码通过构造函数 {{domxref("Worker.Worker", "Worker()")}}  创建了一个 {{domxref("Worker")}} 对象。</p>
+
+<pre class="brush: js notranslate">var myWorker = new Worker('worker.js');
+var first = document.querySelector('#number1');
+var second = document.querySelector('#number2');
+
+first.onchange = function() {
+ myWorker.postMessage([first.value,second.value]);
+ console.log('Message posted to worker');
+}</pre>
+
+<p>完整的示例,请查阅 <a class="external external-icon" href="https://github.com/mdn/simple-web-worker">Basic dedicated worker example</a> (<a class="external external-icon" href="http://mdn.github.io/simple-web-worker/">run dedicated worker</a>).</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', "#worker", "Worker")}}</td>
+ <td>{{Spec2('HTML WHATWG')}}</td>
+ <td></td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+<p>不同类型的worker兼容度不一致,详细参考具体定义的页面。</p>
+
+<p>{{Compat("api.Worker")}}</p>
+
+<h3 id="跨域行为的错误事件">跨域行为的错误事件</h3>
+
+<p>浏览器的早期版本中,加载跨域的执行者脚本导致 <code>SecurityError</code>事件。根据规范的变更,而新版本的浏览器只有{{event("error")}}事件发生。关于如何处理这种事件的更多信息参考 <a href="https://www.fxsitecompat.com/en-CA/docs/2016/loading-cross-origin-worker-now-fires-error-event-instead-of-throwing-worker-in-sandboxed-iframe-no-longer-allowed/">Loading cross-origin worker now fires error event instead of throwing; worker in sandboxed iframe no longer allowed</a>.</p>
+
+<h2 id="相关链接">相关链接</h2>
+
+<ul>
+ <li><a class="internal" href="/En/Using_web_workers" title="en/Using DOM workers">Using web workers</a></li>
+ <li><a href="/En/DOM/Worker/Functions_available_to_workers" title="https://developer.mozilla.org/En/DOM/Worker/Functions_available_to_workers">Functions available to workers</a></li>
+ <li>Other kind of workers: {{ domxref("SharedWorker") }} and <a href="https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker_API">ServiceWorker</a>.</li>
+ <li>Non-standard, Gecko-specific workers: {{ domxref("ChromeWorker") }}, used by extensions.</li>
+</ul>
diff --git a/files/zh-cn/web/api/worker/message_event/index.html b/files/zh-cn/web/api/worker/message_event/index.html
new file mode 100644
index 0000000000..909bfc8434
--- /dev/null
+++ b/files/zh-cn/web/api/worker/message_event/index.html
@@ -0,0 +1,85 @@
+---
+title: 'Worker: message event'
+slug: Web/API/Worker/message_event
+translation_of: Web/API/Worker/message_event
+---
+<div>{{APIRef}}</div>
+
+<div>当 worker 的父级接收到来自其 worker 的消息时,会在 {{domxref('Worker')}} 对象上触发 <code>message</code> 事件。例如:当 worker 通过 <code><a href="/en-US/docs/Web/API/DedicatedWorkerGlobalScope/postMessage">DedicatedWorkerGlobalScope.postMessage()</a></code> 发送了一条消息时。</div>
+
+<div></div>
+
+<div></div>
+
+<table class="properties">
+ <tbody>
+ <tr>
+ <th scope="row">是否冒泡</th>
+ <td>否</td>
+ </tr>
+ <tr>
+ <th scope="row">是否可撤销</th>
+ <td>否</td>
+ </tr>
+ <tr>
+ <th scope="row">接口</th>
+ <td>{{domxref("MessageEvent")}}</td>
+ </tr>
+ <tr>
+ <th scope="row">对应事件处理属性</th>
+ <td><code><a href="/en-US/docs/Web/API/Worker/onmessage">onmessage</a></code></td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="例子">例子</h2>
+
+<p>下面的代码创建了一个 worker 并使用 <code><a href="/en-US/docs/Web/API/EventTarget/addEventListener">addEventListener()</a></code> 监听从 worker 发来的消息:</p>
+
+<pre class="brush: js">const worker = new Worker("static/scripts/worker.js");
+
+worker.addEventListener('message', (event) =&gt; {
+ console.log(`Received message from worker: ${event.data}`)
+});</pre>
+
+<p>另外,也可以使用 <code><a href="/en-US/docs/Web/API/Worker/onmessage">onmessage</a></code> 事件处理属性进行监听:</p>
+
+<pre class="brush: js">const worker = new Worker("static/scripts/worker.js");
+
+worker.onmessage = (event) =&gt; {
+ console.log(`Received message from worker: ${event.data}`)
+};</pre>
+
+<p>worker 使用  <code><a href="/en-US/docs/Web/API/DedicatedWorkerGlobalScope/postMessage">self.postMessage()</a></code> 发出消息:</p>
+
+<pre class="brush: js">// static/scripts/worker.js
+
+self.postMessage('I\'m alive!');</pre>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('HTML WHATWG', 'indices.html#event-message')}}</td>
+ <td>{{Spec2('HTML WHATWG')}}</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+
+
+<p>{{Compat("api.Worker.message_event")}}</p>
+
+<h2 id="另请参阅">另请参阅</h2>
+
+<ul>
+ <li>相关事件: <code><a href="/docs/Web/API/Worker/messageerror_event">messageerror</a></code>.</li>
+ <li><code><a href="/en-US/docs/Web/API/DedicatedWorkerGlobalScope/postMessage">DedicatedWorkerGlobalScope.postMessage()</a></code>.</li>
+</ul>
diff --git a/files/zh-cn/web/api/worker/messageerror_event/index.html b/files/zh-cn/web/api/worker/messageerror_event/index.html
new file mode 100644
index 0000000000..32ebd5d631
--- /dev/null
+++ b/files/zh-cn/web/api/worker/messageerror_event/index.html
@@ -0,0 +1,87 @@
+---
+title: 'Worker: messageerror event'
+slug: Web/API/Worker/messageerror_event
+translation_of: Web/API/Worker/messageerror_event
+---
+<div>{{APIRef}}</div>
+
+<p>当 {{domxref('Worker')}} 对象接收到一条无法被反序列化的消息时, <code>messageerror</code> 事件将在该对象上被触发。</p>
+
+<table class="properties">
+ <tbody>
+ <tr>
+ <th scope="row">是否冒泡</th>
+ <td>否</td>
+ </tr>
+ <tr>
+ <th scope="row">是否可取消</th>
+ <td>否</td>
+ </tr>
+ <tr>
+ <th scope="row">接口</th>
+ <td>{{domxref("MessageEvent")}}</td>
+ </tr>
+ <tr>
+ <th scope="row">对应事件处理属性</th>
+ <td><code><a href="/en-US/docs/Web/API/Worker/onmessageerror">onmessageerror</a></code></td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="例子">例子</h2>
+
+<p>创建一个 worker ,使用 <code><a href="/en-US/docs/Web/API/EventTarget/addEventListener">addEventListener()</a></code> 监听 <code>message</code> 和 <code>messageerror</code> 事件:</p>
+
+<pre class="brush: js">// inside main.js
+
+const worker = new Worker("static/scripts/worker.js");
+
+worker.addEventListener("message", (event) =&gt; {
+ console.error(`Received message from worker: ${event}`);
+});
+
+worker.addEventListener("messageerror", (event) =&gt; {
+ console.error(`Error receiving message from worker: ${event}`);
+});</pre>
+
+<p>同样,可以使用 <code><a href="/en-US/docs/Web/API/WindowEventHandlers/onmessageerror">onmessageerror</a></code> 事件处理属性监听事件:</p>
+
+<pre class="brush: js">// inside main.js
+
+const worker = new Worker("static/scripts/worker.js");
+
+worker.onmessage = (event) =&gt; {
+ console.error(`Received message from worker: ${event}`);
+};
+
+worker.onmessageerror = (event) =&gt; {
+ console.error(`Error receiving message from worker: ${event}`);
+};</pre>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('HTML WHATWG', 'indices.html#event-messageerror')}}</td>
+ <td>{{Spec2('HTML WHATWG')}}</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+
+
+<p>{{Compat("api.Worker.messageerror_event")}}</p>
+
+<h2 id="另请参阅">另请参阅</h2>
+
+<ul>
+ <li><code><a href="/en-US/docs/Web/API/Worker/postMessage">Worker.postMessage()</a></code></li>
+ <li>相关事件: <code><a href="/docs/Web/API/Worker/message_event">message</a></code>.</li>
+</ul>
diff --git a/files/zh-cn/web/api/worker/onmessage/index.html b/files/zh-cn/web/api/worker/onmessage/index.html
new file mode 100644
index 0000000000..9fb062c24f
--- /dev/null
+++ b/files/zh-cn/web/api/worker/onmessage/index.html
@@ -0,0 +1,124 @@
+---
+title: Worker.onmessage
+slug: Web/API/Worker/onmessage
+translation_of: Web/API/Worker/onmessage
+---
+<p>{{APIRef("Web Workers API")}}</p>
+
+<p>{{domxref("Worker")}} 接口的<strong><code>onmessage</code></strong>属性表示一个{{domxref("EventHandler")}}事件处理函数,当{{event("message")}} 事件发生时,该函数被调用。这些事件所属{{domxref("MessageEvent")}}类型,且当Worker子线程返回一条消息时被调用(比如:从{{domxref("DedicatedWorkerGlobalScope.postMessage")}}函数发出的信息)</p>
+
+<div class="note">
+<p><strong>注意</strong>: 消息被装载到 {{event("message")}}事件对象的<code>data</code>属性。(译者:即传递的消息参数将被赋值给<code>onmessage</code>处理函数的事件参数<code>e</code>中的<code>data</code>属性)</p>
+</div>
+
+<h2 id="语法">语法</h2>
+
+<pre class="brush: js notranslate">myWorker.onmessage = function(e) { ... }</pre>
+
+<h2 id="范例">范例</h2>
+
+<p>下面的代码片段示范用{{domxref("Worker.Worker", "Worker()")}} 构造函数创建一个{{domxref("Worker")}}对象。当表单的<code>first</code>输入框的值变更时,消息被传递给worker。<code>myWorker.onmessage</code>函数用来处理从worker回传的消息。</p>
+
+<pre class="brush: js notranslate">var myWorker = new Worker('worker.js');
+
+first.onchange = function() {
+ myWorker.postMessage([first.value,second.value]);
+ console.log('Message posted to worker');
+}
+
+myWorker.onmessage = function(e) {
+  result.textContent = e.data;
+  console.log('Message received from worker');
+}
+</pre>
+
+<p>在<code>worker.js</code>中,  <code>onmessage</code> 函数用来接收从主线程传递过来的信息:</p>
+
+<pre class="brush: js notranslate">onmessage = function(e) {
+ console.log('Message received from main script');
+ var workerResult = 'Result: ' + (e.data[0] * e.data[1]);
+ console.log('Posting message back to main script');
+ postMessage(workerResult);
+}</pre>
+
+<p>请注意,主线程中必须以myWorker.<code>onmessage</code>方式调用,  反之 <code>worker.js</code> 脚本中, 只需定义 <code>onmessage</code>, 因为worker.js全域有效({{domxref("DedicatedWorkerGlobalScope")}}).</p>
+
+<p>完整范例,请参考<a class="external external-icon" href="https://github.com/mdn/simple-web-worker">Basic dedicated worker example</a> (<a class="external external-icon" href="http://mdn.github.io/simple-web-worker/">run dedicated worker</a>).</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', "#handler-worker-onmessage", "Worker.onmessage")}}</td>
+ <td>{{Spec2('HTML WHATWG')}}</td>
+ <td>No change from {{SpecName("Web Workers")}}.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('Web Workers', "#handler-worker-onmessage", "Worker.onmessage")}}</td>
+ <td>{{Spec2('Web Workers')}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+<div>{{CompatibilityTable}}</div>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>特性</th>
+ <th>Chrome</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari (WebKit)</th>
+ </tr>
+ <tr>
+ <td>基本支持</td>
+ <td>4</td>
+ <td>3.5</td>
+ <td>10.0</td>
+ <td>10.6</td>
+ <td>4</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>特性</th>
+ <th>Android</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>Firefox OS (Gecko)</th>
+ <th>IE Phone</th>
+ <th>Opera Mobile</th>
+ <th>Safari Mobile</th>
+ </tr>
+ <tr>
+ <td>基本支持</td>
+ <td>4.4</td>
+ <td>3.5</td>
+ <td>1.0.1</td>
+ <td>10.0</td>
+ <td>11.5</td>
+ <td>5.1</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<h2 id="参阅">参阅</h2>
+
+<p>{{domxref("Worker")}} 接口所属。</p>
diff --git a/files/zh-cn/web/api/worker/onmessageerror/index.html b/files/zh-cn/web/api/worker/onmessageerror/index.html
new file mode 100644
index 0000000000..17783b2e6b
--- /dev/null
+++ b/files/zh-cn/web/api/worker/onmessageerror/index.html
@@ -0,0 +1,43 @@
+---
+title: Worker.onmessageerror
+slug: Web/API/Worker/onmessageerror
+translation_of: Web/API/Worker/onmessageerror
+---
+<div>{{APIRef("HTML DOM")}}</div>
+
+<p>{{domxref("Worker")}} 的 <code><strong>onmessageerror</strong></code> 事件处理器接口是一个{{domxref("EventListener")}}, 在 {{domxref("MessageEvent")}} 类型的事件 <code>messageerror</code> 触发时调用 — 也就是说, 它收到的消息是不能进行序列化的 {{glossary("Deserialization", "deserialized")}}.</p>
+
+<p>{{AvailableInWorkers}}</p>
+
+<h2 id="Syntax">Syntax</h2>
+
+<pre class="syntaxbox">Worker.onmessageerror = function() { ... };</pre>
+
+<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', '#handler-worker-onmessageerror', 'onmessageerror')}}</td>
+ <td>{{Spec2('HTML WHATWG')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+
+
+<p>{{Compat("api.Worker.onmessageerror")}}</p>
+
+<h2 id="相关链接">相关链接</h2>
+
+<ul>
+ <li><a href="/en-US/docs/Web/API/Channel_Messaging_API/Using_channel_messaging">Using channel messaging</a></li>
+</ul>
diff --git a/files/zh-cn/web/api/worker/postmessage/index.html b/files/zh-cn/web/api/worker/postmessage/index.html
new file mode 100644
index 0000000000..bc31892eec
--- /dev/null
+++ b/files/zh-cn/web/api/worker/postmessage/index.html
@@ -0,0 +1,210 @@
+---
+title: Worker.postMessage()
+slug: Web/API/Worker/postMessage
+tags:
+ - Worker
+ - Worker.postMessage()
+ - postMessage()
+translation_of: Web/API/Worker/postMessage
+---
+<p>{{APIRef("Web Workers API")}}</p>
+
+<p>{{domxref("Worker")}} 接口的 <code><strong>postMessage()</strong></code>方法向worker的内部作用域发送一个消息。这接受单个参数,这是要发送给worker的数据。数据可以是由<a href="https://developer.mozilla.org/en-US/docs/Web/Guide/DOM/The_structured_clone_algorithm">结构化克隆</a>算法处理的任何值或JavaScript对象,其包括循环引用。</p>
+
+<p>工作者可以使用 {{domxref("DedicatedWorkerGlobalScope.postMessage")}}  方法将信息发送回生成它的线程。</p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="brush: js">myWorker.postMessage(aMessage, transferList);</pre>
+
+<h3 id="参数">参数</h3>
+
+<dl>
+ <dt><em>aMessage</em></dt>
+ <dd>The object to deliver to the worker; this will be in the data field in the event delivered to the {{domxref("DedicatedWorkerGlobalScope.onmessage")}} handler. This may be any value or JavaScript object handled by the <a href="/en-US/docs/Web/Guide/DOM/The_structured_clone_algorithm" title="http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-interfaces.html#transferable">structured clone</a> algorithm, which includes cyclical references.</dd>
+ <dt><em>transferList</em> {{optional_inline}}</dt>
+ <dd>一个可选的{{domxref("Transferable")}}对象的<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array">数组</a>,用于传递所有权。如果一个对象的所有权被转移,在发送它的上下文中将变为不可用(中止),并且只有在它被发送到的worker中可用。</dd>
+ <dd>可转移对象是如{{domxref("ArrayBuffer")}},{{domxref("MessagePort")}}或{{domxref("ImageBitmap")}}的实例对象。transferList数组中不可传入null。</dd>
+</dl>
+
+<h3 id="Returns">Returns</h3>
+
+<p>Void.</p>
+
+<h2 id="Example">Example</h2>
+
+<p>以下代码显示了如何使用 {{domxref("Worker.Worker", "Worker()")}} 构造函数创建一个Worker对象。当两个表单输入(<code>first</code>和<code>second)</code>中的其中一个的输入值改变时, {{event("change")}} 事件将调用<code>postMessage()</code>把两个input的值发送给当前worker。</p>
+
+<pre class="brush: js">var myWorker = new Worker('worker.js');
+
+first.onchange = function() {
+ myWorker.postMessage([first.value,second.value]);
+ console.log('Message posted to worker');
+}
+
+second.onchange = function() {
+  myWorker.postMessage([first.value,second.value]);
+  console.log('Message posted to worker');
+}
+</pre>
+
+<p>有关完整的示例,请参阅我们的<a class="external external-icon" href="https://github.com/mdn/simple-web-worker">Basic dedicated worker example</a> (<a class="external external-icon" href="http://mdn.github.io/simple-web-worker/">run dedicated worker</a>).</p>
+
+<div class="note">
+<p><strong>Note</strong>: <code>postMessage()</code> 一次只能发送一个对象。如上所示,如果你想传递多个值,可以使用数组。</p>
+</div>
+
+<h3 id="Transfer_Example">Transfer Example</h3>
+
+<p>This example shows a Firefox add-on that transfers an <code>ArrayBuffer</code> from the main thread to the <code>ChromeWorker</code>, and then the <code>ChromeWorker</code> transfers it back to the main thread.</p>
+
+<h4 id="Main_thread_code">Main thread code:</h4>
+
+<pre class="brush: js">var myWorker = new ChromeWorker(self.path + 'myWorker.js');
+
+function handleMessageFromWorker(msg) {
+ console.log('incoming message from worker, msg:', msg);
+ switch (msg.data.aTopic) {
+ case 'do_sendMainArrBuff':
+ sendMainArrBuff(msg.data.aBuf)
+ break;
+ default:
+ throw 'no aTopic on incoming message to ChromeWorker';
+ }
+}
+
+myWorker.addEventListener('message', handleMessageFromWorker);
+
+// Ok lets create the buffer and send it
+var arrBuf = new ArrayBuffer(8);
+console.info('arrBuf.byteLength pre transfer:', arrBuf.byteLength);
+
+myWorker.postMessage(
+    {
+        aTopic: 'do_sendWorkerArrBuff',
+        aBuf: arrBuf // The array buffer that we passed to the transferrable section 3 lines below
+    },
+    [
+        arrBuf // The array buffer we created 9 lines above
+    ]
+);
+
+console.info('arrBuf.byteLength post transfer:', arrBuf.byteLength);
+</pre>
+
+<h4 id="Worker_code">Worker code</h4>
+
+<pre class="brush: js">self.onmessage = function (msg) {
+    switch (msg.data.aTopic) {
+        case 'do_sendWorkerArrBuff':
+                sendWorkerArrBuff(msg.data.aBuf)
+            break;
+        default:
+            throw 'no aTopic on incoming message to ChromeWorker';
+    }
+}
+
+function sendWorkerArrBuff(aBuf) {
+    console.info('from worker, PRE send back aBuf.byteLength:', aBuf.byteLength);
+
+    self.postMessage({aTopic:'do_sendMainArrBuff', aBuf:aBuf}, [aBuf]);
+
+    console.info('from worker, POST send back aBuf.byteLength:', aBuf.byteLength);
+}
+</pre>
+
+<h4 id="Output_logged">Output logged</h4>
+
+<pre>arrBuf.byteLength pre transfer: 8 bootstrap.js:40
+arrBuf.byteLength post transfer: 0 bootstrap.js:42
+
+from worker, PRE send back aBuf.byteLength: 8 myWorker.js:5:2
+
+incoming message from worker, msg: message { ... } bootstrap.js:20
+got back buf in main thread, aBuf.byteLength: 8 bootstrap.js:12
+
+from worker, POST send back aBuf.byteLength: 0 myWorker.js:7:2</pre>
+
+<p><code>byteLength</code> goes to 0 as it is transferred. To see a full working example of this Firefox demo add-on see here: <a href="https://github.com/Noitidart/ChromeWorker/tree/aca57d9cadc4e68af16201bdecbfb6f9a6f9ca6b">GitHub :: ChromeWorker - demo-transfer-arraybuffer</a></p>
+
+<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', "#dom-worker-postmessage", "Worker.postMessage()")}}</td>
+ <td>{{Spec2('HTML WHATWG')}}</td>
+ <td>No change from {{SpecName("Web Workers")}}.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('Web Workers', "#dom-worker-postmessage", "Worker.postMessage()")}}</td>
+ <td>{{Spec2('Web Workers')}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility">Browser compatibility</h2>
+
+<div>{{CompatibilityTable}}</div>
+
+<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 (WebKit)</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>10.0 [1]</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Android</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>Firefox OS (Gecko)</th>
+ <th>IE Phone</th>
+ <th>Opera Mobile</th>
+ <th>Safari Mobile</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>10.0 [1]</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<p>[1] Internet Explorer does not support {{domxref("Transferable")}} objects.</p>
+
+<h2 id="See_also">See also</h2>
+
+<ul>
+ <li>The {{domxref("Worker")}} interface it belongs to.</li>
+</ul>
diff --git a/files/zh-cn/web/api/worker/terminate/index.html b/files/zh-cn/web/api/worker/terminate/index.html
new file mode 100644
index 0000000000..f49f8f2c91
--- /dev/null
+++ b/files/zh-cn/web/api/worker/terminate/index.html
@@ -0,0 +1,106 @@
+---
+title: Worker.terminate()
+slug: Web/API/Worker/terminate
+translation_of: Web/API/Worker/terminate
+---
+<p>{{APIRef("Web Workers API")}}</p>
+
+<p>{{domxref("Worker")}} 接口中的 <code><strong>terminate()</strong></code>  方法用于立即终止 {{domxref("Worker")}} 的行为. 本方法并不会等待 worker 去完成它剩余的操作;worker 将会被立刻停止</p>
+
+<h2 id="Syntax">Syntax</h2>
+
+<pre class="brush: js">myWorker.terminate();</pre>
+
+<h3 id="参数">参数</h3>
+
+<p>没有。</p>
+
+<h3 id="返回值">返回值</h3>
+
+<p>Void.</p>
+
+<h2 id="Example">Example</h2>
+
+<p>以下代码示例表明,通过使用 {{domxref("Worker.Worker", "Worker()")}} 构造器创建出的{{domxref("Worker")}} 对象,在下一步操作之后会被立即终止。</p>
+
+<pre class="brush: js">var myWorker = new Worker('worker.js');
+
+myWorker.terminate();
+</pre>
+
+<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', "#dom-worker-terminate", "Worker.terminate()")}}</td>
+ <td>{{Spec2('HTML WHATWG')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility">Browser compatibility</h2>
+
+<div>{{CompatibilityTable}}</div>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Chrome</th>
+ <th>Edge</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari (WebKit)</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>4</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>3.5</td>
+ <td>10.0</td>
+ <td>10.6</td>
+ <td>4</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Android</th>
+ <th>Edge</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>Firefox OS (Gecko)</th>
+ <th>IE Phone</th>
+ <th>Opera Mobile</th>
+ <th>Safari Mobile</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>4.4</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>3.5</td>
+ <td>1.0.1</td>
+ <td>10.0</td>
+ <td>11.5</td>
+ <td>5.1</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<h2 id="See_also">See also</h2>
+
+<p>{{domxref("Worker")}} 接口。</p>
diff --git a/files/zh-cn/web/api/worker/worker/index.html b/files/zh-cn/web/api/worker/worker/index.html
new file mode 100644
index 0000000000..1dc6244203
--- /dev/null
+++ b/files/zh-cn/web/api/worker/worker/index.html
@@ -0,0 +1,112 @@
+---
+title: Worker()
+slug: Web/API/Worker/Worker
+tags:
+ - API
+ - Constructor
+ - Reference
+ - Web Workers
+ - Worker
+ - Worker()
+translation_of: Web/API/Worker/Worker
+---
+<p>{{APIRef("Web Workers API")}}</p>
+
+<p><code><strong>Worker()</strong></code> 构造函数创建一个 {{domxref("Worker")}} 对象,该对象执行指定的URL脚本。这个脚本必须遵守 <a href="/en/Same_origin_policy_for_JavaScript" title="Same origin policy for JavaScript">同源策略</a> 。</p>
+
+<p>如果 此URL有一个无效的语句,或者违反同源策略,一个 <code>SECURITY_ERR </code>类型的{{domxref("DOMException")}}被抛出。</p>
+
+<div class="note">
+<p><strong>Note</strong>: 浏览器厂商对于 data URI 是否同源存在分歧。尽管 Gecko 10.0 {{ geckoRelease("10.0") }} 和之后的版本接受 data URIs,但在所有其他浏览器中并非如此。</p>
+</div>
+
+<h2 id="语法">语法</h2>
+
+<pre class="brush: js">const myWorker = new Worker(aURL, options);</pre>
+
+<h3 id="参数">参数</h3>
+
+<ul>
+ <li>如果文档不允许启动worker,则会引发SecurityError</li>
+ <li>如果脚本之一的MIME类型为 <code>text/csv</code>, <code>image/*</code>, <code>video/*</code>,或 <code>audio/*</code>, 则会引发NetworkError。它应该始终是 text/javascript。</li>
+ <li>如果aURL无法解析,则引发SyntaxError。</li>
+</ul>
+
+<dl>
+ <dt><em>aURL</em></dt>
+ <dd>是一个{{domxref("DOMString")}} 表示<strong>worker </strong>将执行的脚本的URL。它必须遵守同源策略。</dd>
+ <dt><em>options</em> {{optional_inline}}</dt>
+ <dd>包含可在创建对象实例时设置的选项属性的对象。可用属性如下:
+ <ul>
+ <li><code>type</code>:用以指定 worker 类型的  {{domxref("DOMString")}} 值. 该值可以是 <code>classic</code> 或 <code>module</code>. 如果未指定,将使用默认值 <code>classic.</code></li>
+ <li><code>credentials</code>:用以指定 worker 凭证的 {{domxref("DOMString")}} 值.该值可以是<dfn> <code>omit</code></dfn>, <code><dfn>same-origin</dfn></code>,或 <dfn><code>include</code>.。如果未指定,或者 type 是 <code>classic</code>,将使用默认值 <code>omit</code> (不要求凭证)。</dfn></li>
+ <li><dfn><code>name</code>:</dfn>在 {{domxref("DedicatedWorkerGlobalScope")}} 的情况下,用来表示 worker 的 scope 的一个<dfn> </dfn>{{domxref("DOMString")}} 值,主要用于调试目的。</li>
+ </ul>
+ </dd>
+</dl>
+
+<h3 id="返回值">返回值</h3>
+
+<p>创建的 worker。</p>
+
+<h3 id="异常">异常</h3>
+
+<ul>
+ <li>当 document 不被允许启动 worker 的时候,将抛出一个 SecurityError 异常。例如:如果提供的 aURL 有语法错误,或者与同源策略相冲突(跨域访问)。</li>
+ <li>如果 worker 的 MIME 类型不正确,将抛出一个 NetworkError 异常。worker 的 MIME 类型必须是 text/javascript 。</li>
+ <li>如果 aURL 无法被解析(格式错误),将抛出一个 SyntaxError 异常。</li>
+</ul>
+
+<h2 id="例子">例子</h2>
+
+<p>下面的代码片段展示了通过 Worker() 创建 {{domxref("Worker")}} 对象的过程, 以及随后的使用方法:</p>
+
+<pre class="brush: js">let myWorker = new Worker("worker.js");
+
+first.onchange = function() {
+ myWorker.postMessage([first.value,second.value]);
+ console.log('Message posted to worker');
+}</pre>
+
+<p>完整的例子请看 <a class="external external-icon" href="https://github.com/mdn/simple-web-worker">Basic dedicated worker example</a> (<a class="external external-icon" href="http://mdn.github.io/simple-web-worker/">run dedicated worker</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-worker", "Worker()")}}</td>
+ <td>{{Spec2('HTML WHATWG')}}</td>
+ <td>No change from {{SpecName("Web Workers")}}.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('Web Workers', "#dom-worker", "Worker()")}}</td>
+ <td>{{Spec2('Web Workers')}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+<div>{{Compat("api.Worker.Worker")}}</div>
+
+<div class="blockIndicator note">
+<p><strong>Note</strong>: 浏览器可以被标记为对<code>Worker()</code>的完全支持尽管他并不支持一个以modules类型编写的脚本。截至2019年8月1日,暂无浏览器支持以模块类型编写的脚本。如果没有这种支持,moduleds类型的脚本必须使用编译器翻译成无module代码才能在浏览器上运行。</p>
+</div>
+
+
+
+<div></div>
+
+<div>另请参阅</div>
+
+<ul>
+ <li> {{domxref("Worker")}} 它所属的接口。</li>
+ <li><a href="https://developer.mozilla.org/zh-CN/docs/Web/API/Web_Workers_API">https://developer.mozilla.org/zh-CN/docs/Web/API/Web_Workers_API</a></li>
+</ul>