这篇翻译不完整。请帮忙从英语翻译这篇文章

Worker() 构造函数创建一个 Worker 对象,该对象执行指定的URL脚本。这个脚本必须遵守 同源策略 。

如果 此URL有一个无效的语句,或者违反同源策略,一个 SECURITY_ERR 类型的DOMException被抛出。

Note: 浏览器厂商对于 data URI 是否同源存在分歧。尽管 Gecko 10.0 (Firefox 10.0 / Thunderbird 10.0 / SeaMonkey 2.7) 和之后的版本接受 data URIs,但在所有其他浏览器中并非如此。

语法

const myWorker = new Worker(aURL, options);

参数

  • 如果文档不允许启动worker,则会引发SecurityError
  • 如果脚本之一的MIME类型为 text/csvimage/*video/*,或 audio/*, 则会引发NetworkError。它应该始终是 text/javacript。
  • 如果aURL无法解析,则引发SyntaxError。
aURL
是一个DOMString 表示worker 将执行的脚本的URL。它必须遵守同源策略。
options 可选
An object containing option properties that can be set when creating the object instance. Available properties are as follows:
  • type: A DOMString specifying the type of worker to create. The value can be classic or module. If not specified, the default used is classic.
  • credentials: A DOMString specifying the type of credentials to use for the worker. The value can be omitsame-origin, or include. If not specified, or if type is classic, the default used is omit (no credentials required).
  • name: A DOMString specifying an identifying name for the DedicatedWorkerGlobalScope representing the scope of the worker, which is mainly useful for debugging purposes.

返回值

创建的 worker。

例外

例子

下面的代码片段展示了通过 Worker() 创建 Worker 对象的过程, 以及随后的使用方法:

let myWorker = new Worker("worker.js");

first.onchange = function() {
  myWorker.postMessage([first.value,second.value]);
  console.log('Message posted to worker');
}

完整的例子请看 Basic dedicated worker example (run dedicated worker).

规范

Specification Status Comment
HTML Living Standard
Worker()
Living Standard No change from Unknown.
Unknown
Worker()
Unknown Initial definition.

浏览器兼容性

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 4 3.5 10.0 10.6 4
Feature Android Firefox Mobile (Gecko) Firefox OS (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support 4.4 3.5 1.0.1 10.0 11.5 5.1

另请参阅

文档标签和贡献者

此页面的贡献者: xgqfrms-GitHub, liuzeyafzy, Alphmega
最后编辑者: xgqfrms-GitHub,