DedicatedWorkerGlobalScope

翻译不完整。 请帮助我们翻译这篇文章!

DedicatedWorkerGlobalScope 对象(也就是 Worker 全局作用域)可以通过 self 关键字来访问。一些在 worker 全局作用域下不可用的全局函数、命名空间对象以及构造器,也可以通过此对象使用。在 JavaScript 参考Web Workers 可以使用的函数和类页面中,有列举这些特性。

属性

该接口从 WorkerGlobalScope 接口以及它的父接口 EventTarget 继承属性,因此,此接口也实现了来自 WindowTimersWindowBase64 和WindowEventHandlers 的属性。

DedicatedWorkerGlobalScope.name 只读
通过 Worker() 构造器创建 Worker 时,可以选择为其设置一个名称,即此属性的值。这主要用于调试。

从 WorkerGlobalScope 继承的属性

WorkerGlobalScope.self
返回一个指向 DedicatedWorkerGlobalScope 本身的对象引用。
WorkerGlobalScope.console 只读
返回与当前 worker 相关联的 Console
WorkerGlobalScope.location 只读
返回与当前 worker 相关联的 WorkerLocationWorkerLocation 是一个 worker 专有的 location 对象,基本上是浏览器作用域下 Location 的子集,但被被适配给了 worker。
WorkerGlobalScope.navigator 只读
返回与当前 worker 相关联的 WorkerNavigatorWorkerNavigator 是一个 worker 专有的 navigator 对象,基本上是浏览器作用域下 Navigator 的子集,但被被适配给了 worker。
WorkerGlobalScope.performance 只读
返回与当前 worker 相关联的 Performance,是一个正常的 performance 对象,但只有一部分属性和方法可用。

事件处理器

该接口从 WorkerGlobalScope 接口以及它的父接口 EventTarget 继承事件处理器,因此,此接口也实现了来自 WindowTimersWindowBase64 和WindowEventHandlers 的事件处理器。

DedicatedWorkerGlobalScope.onmessage
Is an EventHandler representing the code to be called when the message event is raised. These events are of type MessageEvent and will be called when the worker receives a message from the document that started it (i.e. from the Worker.postMessage method.)
DedicatedWorkerGlobalScope.onmessageerror
Is an EventHandler representing the code to be called when the messageerror event is raised.

方法

该接口从 WorkerGlobalScope 接口以及它的父接口 EventTarget 继承方法,因此,此接口也实现了来自 WindowTimersWindowBase64 和WindowEventHandlers 的方法。

WorkerGlobalScope.close()
抛弃当前 WorkerGlobalScope 的 event loop 中所有正在排队的任务,关闭当前作用域。
DedicatedWorkerGlobalScope.postMessage
向该 worker 的父文档发送消息——消息可以是任何 Javascript 对象。

从 WorkerGlobalScope 继承的方法

WorkerGlobalScope.dump()
向控制台写入一条消息。
WorkerGlobalScope.importScripts()
向当前 worker 的作用域导入一或更多条脚本。可按需导入任意数量的脚本,使用逗号分割参数。比如: importScripts('foo.js', 'bar.js');

从其他来源实现的方法

WindowBase64.atob()
解码使用 base-64 编码的字符串数据。
WindowBase64.btoa()
从字符串生成使用 base-64 编码的 ASCII 字符串。
WindowTimers.clearInterval()
取消使用 WindowTimers.setInterval() 创建的定时操作。
WindowTimers.clearTimeout()
取消使用 WindowTimers.setTimeout() 创建的定时操作。
WindowTimers.setInterval()
每隔一定时间执行一次给定函数。
WindowTimers.setTimeout()
延迟一定时间执行给定函数。

事件

message
Fired when the worker receives a message from its parent.
Also available via the onmessage property.
messageerror
Fired when a worker receives a message that can't be deserialized.
Also available via the onmessageerror property.

规范

规范 状态 备注
HTML Living Standard
DedicatedWorkerGlobalScope
Living Standard

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
DedicatedWorkerGlobalScopeChrome Full support 4Edge Full support 12Firefox Full support 3.5IE Full support 10Opera Full support 10.6Safari Full support 4WebView Android Full support YesChrome Android Full support 18Firefox Android Full support 4Opera Android Full support 11Safari iOS Full support 5.1Samsung Internet Android Full support 1.0
closeChrome Full support YesEdge Full support ≤79Firefox Full support 54IE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 54Opera Android Full support YesSafari iOS ? Samsung Internet Android Full support Yes
message eventChrome Full support 4Edge Full support 12Firefox Full support 3.5IE Full support 10Opera Full support 10.6Safari Full support 4WebView Android Full support 37Chrome Android Full support YesFirefox Android Full support 4Opera Android Full support 11.5Safari iOS Full support 5.1Samsung Internet Android Full support Yes
messageerror eventChrome Full support 60Edge Full support ≤79Firefox Full support 57IE ? Opera Full support 47Safari ? WebView Android Full support 60Chrome Android Full support 60Firefox Android Full support 57Opera Android Full support 47Safari iOS ? Samsung Internet Android Full support 8.0
nameChrome Full support YesEdge Full support ≤79Firefox Full support 55IE No support NoOpera ? Safari No support NoWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 55Opera Android ? Safari iOS No support NoSamsung Internet Android Full support Yes
onmessageChrome Full support 4Edge Full support 12Firefox Full support 3.5IE Full support 10Opera Full support 10.6Safari Full support 4WebView Android Full support 37Chrome Android Full support YesFirefox Android Full support 4Opera Android Full support 11Safari iOS Full support 5.1Samsung Internet Android Full support Yes
onmessageerrorChrome Full support 60Edge Full support ≤79Firefox Full support 57IE ? Opera Full support 47Safari ? WebView Android Full support 60Chrome Android Full support 60Firefox Android Full support 57Opera Android Full support 44Safari iOS ? Samsung Internet Android Full support 8.0
postMessageChrome Full support 4Edge Full support 12Firefox Full support 3.5IE Full support 10Opera Full support 10.6Safari Full support 4WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support 11Safari iOS Full support 5.1Samsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown

参见