--- title: WorkerGlobalScope slug: Web/API/WorkerGlobalScope tags: - API - Interface - NeedsBrowserCompatibility - NeedsTranslation - Reference - TopicStub - WorkerGlobalScope - Workers translation_of: Web/API/WorkerGlobalScope ---
{{APIRef("Web Workers API")}}
Web Workers API 的 WorkerGlobalScope
接口 是一个代表了任何 scope of worker的接口. Workers 没有浏览内容; 这个 scope 包含的信息总是通过 {{domxref("Window")}} objects传递 — 比如 event handlers, the console or the associated {{domxref("WorkerNavigator")}} object.每个 WorkerGlobalScope
都有自己的事件循环.
每个 worker type都有专门的这个接口: {{domxref("DedicatedWorkerGlobalScope")}} for dedicated workers, {{domxref("SharedWorkerGlobalScope")}} for shared workers, and {{domxref("ServiceWorkerGlobalScope")}} for ServiceWorker. self
属性返回每个内容的专门 scope .
This interface inherits properties from the {{domxref("EventTarget")}} interface and implements properties from {{domxref("WindowTimers")}}, {{domxref("WindowBase64")}}, and {{domxref("WindowEventHandlers")}}.
WorkerGlobalScope
itself. Most of the time it is a specific scope like {{domxref("DedicatedWorkerGlobalScope")}}, {{domxref("SharedWorkerGlobalScope")}} or {{domxref("ServiceWorkerGlobalScope")}}.This interface inherits event handlers from the {{domxref("EventTarget")}} interface and implements event handlers from {{domxref("WindowTimers")}}, and {{domxref("WindowBase64")}}.
Promise
rejection events.Promise
rejection events.This interface inherits methods from the {{domxref("EventTarget")}} interface and implements methods from {{domxref("WindowTimers")}}, {{domxref("WindowBase64")}}, {{domxref("WindowEventHandlers")}}, and {{domxref("GlobalFetch")}}.
WorkerGlobalScope
's event loop, effectively closing this particular scope. importScripts('foo.js', 'bar.js');
You won't access WorkerGlobalScope
directly in your code; however, its properties and methods are inherited by more specific global scopes such as {{domxref("DedicatedWorkerGlobalScope")}} and {{domxref("SharedWorkerGlobalScope")}}. For example, you could import another script into the worker and print out the contents of the worker scope's navigator
object using the following two lines:
importScripts('foo.js'); console.log(navigator);
Note: Since the global scope of the worker script is effectively the global scope of the worker you are running ({{domxref("DedicatedWorkerGlobalScope")}} or whatever) and all worker global scopes inherit methods, properties, etc. from WorkerGlobalScope
, you can run lines such as those above without specifying a parent object.
Specification | Status | Comment |
---|---|---|
{{SpecName('HTML WHATWG', '#workerglobalscope', 'WorkerGlobalScope')}} | {{Spec2('HTML WHATWG')}} | No change from {{SpecName("Web Workers")}}. |
{{SpecName('Service Workers')}} | {{Spec2('Service Workers')}} | Defines caches . |
{{SpecName('Web Workers', '#workerglobalscope', 'WorkerGlobalScope')}} | {{Spec2('Web Workers')}} | Initial definition |
{{CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | {{CompatChrome(4)}} | {{CompatGeckoDesktop("1.9.1")}} | 10 | 10.6 | 4 |
ononline , onoffline |
{{CompatVersionUnknown}} | {{CompatGeckoDesktop("29")}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
console {{Non-standard_inline}} |
{{CompatVersionUnknown}} | {{CompatGeckoDesktop("29")}}[1] {{CompatGeckoDesktop("30")}} |
{{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
performance |
{{CompatVersionUnknown}} | {{CompatGeckoDesktop("34")}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
caches |
{{CompatChrome(40)}} | {{CompatUnknown}} | {{CompatNo}} | {{CompatUnknown}} | {{CompatNo}} |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | Firefox OS (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | {{CompatUnknown}} | {{CompatVersionUnknown}} | {{CompatGeckoMobile("1.9.1")}} | 1.0.1 | 10 | 11.5 | 5.1 |
ononline , onoffline |
{{CompatUnknown}} | {{CompatVersionUnknown}} | {{CompatGeckoMobile("29")}}[1] {{CompatGeckoMobile("30")}} |
1.4 | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
console {{Non-standard_inline}} |
{{CompatUnknown}} | {{CompatVersionUnknown}} | {{CompatGeckoMobile("29")}} | 1.4 | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
performance |
{{CompatUnknown}} | {{CompatVersionUnknown}} | {{CompatGeckoMobile("34")}} | 2.1 | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
caches |
{{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
[1] Gecko 29 implemented this as WorkerConsole
. Since version 30 it uses the regular Console
.