WorkerGlobalScope

WorkerGlobalScope interface of the Web 工作者 API is an interface representing the scope of any worker. Workers have no browsing context; this scope contains the information usually conveyed by Window objects — in this case event handlers, the console or the associated WorkerNavigator object. Each WorkerGlobalScope has its own event loop.

This interface is usually specialized by each worker type: DedicatedWorkerGlobalScope for dedicated workers, SharedWorkerGlobalScope for shared workers, and ServiceWorkerGlobalScope for ServiceWorker self property returns the specialized scope for each context.

特性

此接口继承的特性来自 EventTarget interface and WindowOrWorkerGlobalScope and WindowEventHandlers mixins.

Standard properties

WorkerGlobalScope.navigator 只读

返回 WorkerNavigator associated with the worker. It is a specific navigator object, mostly a subset of the Navigator for browsing scopes, but adapted to workers.
WorkerGlobalScope.self 只读
Returns a reference to the WorkerGlobalScope itself. Most of the time it is a specific scope like DedicatedWorkerGlobalScope , SharedWorkerGlobalScope or ServiceWorkerGlobalScope .
WorkerGlobalScope.location 只读
返回 WorkerLocation associated with the worker. It is a specific location object, mostly a subset of the Location for browsing scopes, but adapted to workers.

Non-standard properties

WorkerGlobalScope.performance 只读
返回 Performance associated with the worker. It is a regular performance object, except that only a subset of its property and methods are available to workers.
WorkerGlobalScope.console 只读
返回 Console associated with the worker.

从其它地方实现的特性

WindowOrWorkerGlobalScope.caches 只读
返回 CacheStorage object associated with the current context. This object enables functionality such as storing assets for offline use, and generating custom responses to requests.
WindowOrWorkerGlobalScope.indexedDB 只读
Provides a mechanism for applications to asynchronously access capabilities of indexed databases; returns an IDBFactory 对象。
WindowOrWorkerGlobalScope.isSecureContext 只读
Returns a boolean indicating whether the current context is secure ( true ) or not ( false ).
WindowOrWorkerGlobalScope.origin 只读
Returns the global object's origin, serialized as a string. (This does not yet appear to be implemented in any browser.)

Events

error
Fired when an error occured.
Also available via the WorkerGlobalScope.onerror 特性。
offline
Fired when the browser has lost access to the network and the value of navigator.onLine switched to false .
Also available via the WorkerGlobalScope.onoffline 特性。
online
Fired when the browser has gained access to the network and the value of navigator.onLine switched to true .
Also available via the WorkerGlobalScope.ononline 特性。
languagechange
Fired at the global/worker scope object when the user's preferred languages change.
Also available via the WorkerGlobalScope.onlanguagechange 特性。
close
Is an EventHandler representing the code to be called when the close event is raised.
Also available via the WorkerGlobalScope.onclose 特性。
rejectionhandled
An event handler for handled Promise rejection events.
Also available via the WorkerGlobalScope.onrejectionhandled 特性。
unhandledrejection
An event handler for unhandled Promise rejection events.
Also available via the WorkerGlobalScope.onunhandledrejection 特性。

方法

此接口继承的方法来自 EventTarget interface and WindowOrWorkerGlobalScope and WindowEventHandlers mixins.

Standard methods

WorkerGlobalScope.importScripts()
Imports one or more scripts into the worker's scope. You can specify as many as you'd like, separated by commas. For example: importScripts('foo.js', 'bar.js');

Non-standard methods

WorkerGlobalScope.dump()
Allows you to write a message to stdout — i.e. in your terminal. This is the same as Firefox's window.dump , but for workers.

从其它地方实现的方法

WindowOrWorkerGlobalScope.atob()
Decodes a string of data which has been encoded using base-64 encoding.
WindowOrWorkerGlobalScope.btoa()
Creates a base-64 encoded ASCII string from a string of binary data.
WindowOrWorkerGlobalScope.clearInterval()
Cancels the repeated execution set using WindowOrWorkerGlobalScope.setInterval() .
WindowOrWorkerGlobalScope.clearTimeout()
Cancels the delayed execution set using WindowOrWorkerGlobalScope.setTimeout() .
WindowOrWorkerGlobalScope.createImageBitmap()
Accepts a variety of different image sources, and returns a Promise which resolves to an ImageBitmap . Optionally the source is cropped to the rectangle of pixels originating at (sx, sy) with width sw, and height sh.
WindowOrWorkerGlobalScope.fetch()
Starts the process of fetching a resource from the network.
WindowOrWorkerGlobalScope.setInterval()
Schedules a function to execute every time a given number of milliseconds elapses.
WindowOrWorkerGlobalScope.setTimeout()
Schedules a function to execute in a given amount of time.

弃用方法

WorkerGlobalScope.close()
Discards any tasks queued in the WorkerGlobalScope 's event loop, effectively closing this particular scope. In newer browser versions, close() is available on DedicatedWorkerGlobalScope and SharedWorkerGlobalScope instead. This change was made to stop close() being available on service workers, as it isn't supposed to be used there and always throws an exception when called (see bug 1336043 ).

范例

You won't access WorkerGlobalScope directly in your code; however, its properties and methods are inherited by more specific global scopes such as DedicatedWorkerGlobalScope and 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);
					

Since the global scope of the worker script is effectively the global scope of the worker you are running ( 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.

规范

规范 状态 Comment
HTML Living Standard
The definition of 'WorkerGlobalScope' in that specification.
实时标准

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
WorkerGlobalScope Chrome 4 Edge 12 Firefox 3.5 IE 10 Opera 10.6 Safari 4 WebView Android Yes Chrome Android Yes Firefox Android 4 Opera Android 11 Safari iOS 3.2 Samsung Internet Android Yes
close
弃用 非标
Chrome 4 Edge 12 Firefox 3.5 IE Yes Opera 11.5 Safari 4 WebView Android 37 Chrome Android Yes Firefox Android 4 Opera Android ? Safari iOS ? Samsung Internet Android Yes
console
弃用 非标
Chrome 4 Edge 12 Firefox 29
29
Before Firefox 30, the console would be of type WorkerConsole instead of Console .
IE Yes Opera Yes Safari Yes WebView Android 37 Chrome Android Yes Firefox Android 29
29
Before Firefox 30, the console would be of type WorkerConsole instead of Console .
Opera Android ? Safari iOS ? Samsung Internet Android Yes
dump
弃用 非标
Chrome No Edge No Firefox No IE No Opera No Safari No WebView Android No Chrome Android No Firefox Android 4 Opera Android No Safari iOS No Samsung Internet Android No
importScripts Chrome 4 Edge 12 Firefox 4 IE 10 Opera 10.6 Safari 4 WebView Android ≤37 Chrome Android 18 Firefox Android 4 Opera Android 11 Safari iOS 3.2 Samsung Internet Android 1.0
languagechange event Chrome 4 Edge 12 Firefox 74 IE Yes Opera 11.5 Safari 4 WebView Android ? Chrome Android 40 Firefox Android No Opera Android Yes Safari iOS 5.1 Samsung Internet Android 4.0
location Chrome 4 Edge 12 Firefox 3.5 IE Yes Opera 11.5 Safari 4 WebView Android 37 Chrome Android 40 Firefox Android 4 Opera Android Yes Safari iOS 5.1 Samsung Internet Android 4.0
navigator Chrome 4 Edge 17 Firefox 3.5 IE Yes Opera 11.5 Safari 4 WebView Android 37 Chrome Android 40 Firefox Android 4 Opera Android Yes Safari iOS 5.1 Samsung Internet Android 4.0
onclose
弃用 非标
Chrome 4 Edge 12 Firefox 3.5 — 50 IE Yes Opera 11.5 Safari 4 WebView Android 37 Chrome Android 40 Firefox Android 4 — 50 Opera Android Yes Safari iOS 5.1 Samsung Internet Android 4.0
onerror Chrome 4 Edge 12 Firefox 3.5 IE Yes Opera 11.5 Safari 4 WebView Android 37 Chrome Android 40 Firefox Android 4 Opera Android Yes Safari iOS 5.1 Samsung Internet Android 4.0
onlanguagechange Chrome 4 Edge 12 Firefox 74 IE Yes Opera 11.5 Safari 4 WebView Android 37 Chrome Android 40 Firefox Android No Opera Android Yes Safari iOS 5.1 Samsung Internet Android 4.0
onoffline Chrome 4 Edge ≤79 Firefox 29 IE ? Opera ? Safari ? WebView Android Yes Chrome Android 40 Firefox Android 29 Opera Android ? Safari iOS ? Samsung Internet Android 4.0
ononline Chrome 4 Edge ≤79 Firefox 29 IE ? Opera ? Safari ? WebView Android Yes Chrome Android 40 Firefox Android 29 Opera Android ? Safari iOS ? Samsung Internet Android 4.0
performance Chrome Yes Edge ≤79 Firefox 34 IE ? Opera ? Safari ? WebView Android Yes Chrome Android Yes Firefox Android 34 Opera Android ? Safari iOS ? Samsung Internet Android Yes
self Chrome 4 Edge 12 Firefox 3.5 IE Yes Opera 11.5 Safari 4 WebView Android 37 Chrome Android 40 Firefox Android 34 Opera Android Yes Safari iOS 5.1 Samsung Internet Android 4.0

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
非标。预期跨浏览器支持较差。
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
弃用。不要用于新网站。
见实现注意事项。

另请参阅