DedicatedWorkerGlobalScope

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

属性

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

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 的事件处理器。

DedicatedWorkerGlobalScope.onmessage

Is an event handler 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 event handler representing the code to be called when the messageerror event is raised.

方法

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

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.

规范

Specification
HTML Standard
# dedicated-workers-and-the-dedicatedworkerglobalscope-interface

浏览器兼容性

BCD tables only load in the browser

参见