DedicatedWorkerGlobalScope
DedicatedWorkerGlobalScope
对象(也就是 Worker
全局作用域)可以通过 self
关键字来访问。一些在 worker 全局作用域下不可用的全局函数、命名空间对象以及构造器,也可以通过此对象使用。在 JavaScript 参考的 Web Workers 可以使用的函数和类 (en-US)页面中,有列举这些特性。
属性
该接口从 WorkerGlobalScope
接口以及它的父接口 EventTarget
继承属性,因此,此接口也实现了来自 WindowTimers
、WindowBase64
的属性。
从 WorkerGlobalScope 继承的属性
WorkerGlobalScope.self
-
返回一个指向
DedicatedWorkerGlobalScope
本身的对象引用。 WorkerGlobalScope.console
(en-US) 只读-
返回与当前 worker 相关联的
Console
。 WorkerGlobalScope.location
(en-US) 只读-
返回与当前 worker 相关联的
WorkerLocation
(en-US)。WorkerLocation
是一个 worker 专有的 location 对象,基本上是浏览器作用域下Location
的子集,但被被适配给了 worker。 -
返回与当前 worker 相关联的
WorkerNavigator
(en-US)。WorkerNavigator
是一个 worker 专有的 navigator 对象,基本上是浏览器作用域下Navigator
的子集,但被被适配给了 worker。 WorkerGlobalScope.performance
(en-US) 只读 非标准-
返回与当前 worker 相关联的
Performance
,是一个正常的 performance 对象,但只有一部分属性和方法可用。
事件处理器
该接口从 WorkerGlobalScope
接口以及它的父接口 EventTarget
继承事件处理器,因此,此接口也实现了来自 WindowTimers
、WindowBase64
的事件处理器。
DedicatedWorkerGlobalScope.onmessage
(en-US)-
Is an event handler representing the code to be called when the
message
event is raised. These events are of typeMessageEvent
and will be called when the worker receives a message from the document that started it (i.e. from theWorker.postMessage
method.) DedicatedWorkerGlobalScope.onmessageerror
(en-US)-
Is an event handler representing the code to be called when the
messageerror
(en-US) event is raised.
方法
该接口从 WorkerGlobalScope
接口以及它的父接口 EventTarget
继承方法,因此,此接口也实现了来自 WindowTimers
、WindowBase64
的方法。
WorkerGlobalScope.close()
(en-US)-
抛弃当前
WorkerGlobalScope
的 event loop 中所有正在排队的任务,关闭当前作用域。 DedicatedWorkerGlobalScope.postMessage
(en-US)-
向该 worker 的父文档发送消息——消息可以是任何 Javascript 对象。
从 WorkerGlobalScope 继承的方法
WorkerGlobalScope.dump()
(en-US) 非标准-
向控制台写入一条消息。
WorkerGlobalScope.importScripts()
-
向当前 worker 的作用域导入一或更多条脚本。可按需导入任意数量的脚本,使用逗号分割参数。比如:
importScripts('foo.js', 'bar.js');
从其他来源实现的方法
WindowBase64.atob()
-
解码使用 base-64 编码的字符串数据。
WindowBase64.btoa()
-
从字符串生成使用 base-64 编码的 ASCII 字符串。
WindowTimers.clearInterval()
(en-US)-
取消使用
WindowTimers.setInterval()
(en-US) 创建的定时操作。 WindowTimers.clearTimeout()
-
取消使用
WindowTimers.setTimeout()
(en-US) 创建的定时操作。 WindowTimers.setInterval()
(en-US)-
每隔一定时间执行一次给定函数。
WindowTimers.setTimeout()
(en-US)-
延迟一定时间执行给定函数。
事件
message
(en-US)-
Fired when the worker receives a message from its parent. Also available via the
onmessage
(en-US) property. messageerror
(en-US)-
Fired when a worker receives a message that can't be deserialized. Also available via the
onmessageerror
(en-US) property.
规范
Specification |
---|
HTML Standard # dedicated-workers-and-the-dedicatedworkerglobalscope-interface |
浏览器兼容性
BCD tables only load in the browser