WorkerGlobalScope

Web Workers APIWorkerGlobalScope 接口 是一个代表了任何 scope of worker 的接口. Workers 没有浏览内容;这个 scope 包含的信息总是通过 Window objects 传递 — 比如 event handlers, the console or the associated WorkerNavigator (en-US) object。每个 WorkerGlobalScope 都有自己的事件循环。

每个 worker type 都有专门的这个接口:DedicatedWorkerGlobalScope for dedicated workers, SharedWorkerGlobalScope (en-US) for shared workers, and ServiceWorkerGlobalScope for ServiceWorker (en-US). self 属性返回每个内容的专门 scope.

Properties

This interface inherits properties from the EventTarget interface and implements properties from WindowTimers, WindowBase64, and WindowEventHandlers.

Standard properties

WorkerGlobalScope.caches (en-US) 只读

Returns the CacheStorage object associated with the current context. This object enables service worker (en-US) functionality such as storing assets for offline use, and generating custom responses to requests.

WorkerGlobalScope.navigator (en-US) 只读

Returns the WorkerNavigator (en-US) 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 (en-US) or ServiceWorkerGlobalScope.

WorkerGlobalScope.location (en-US) 只读

Returns the WorkerLocation (en-US) 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 (en-US) 只读

Returns the 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 (en-US) 只读 非标准

Returns the Console associated with the worker.

Event Handlers

This interface inherits event handlers from the EventTarget interface and implements event handlers from WindowTimers, and WindowBase64.

WorkerGlobalScope.onerror (en-US)

Is an event handler representing the code to be called when the error (en-US) event is raised.

WorkerGlobalScope.onoffline (en-US)

Is an event handler representing the code to be called when the offline (en-US) event is raised.

WorkerGlobalScope.ononline (en-US)

Is an event handler representing the code to be called when the online (en-US) event is raised.

WorkerGlobalScope.onlanguagechange (en-US)

An event handler fired at the global/worker scope object when the user's preferred languages change.

WorkerGlobalScope.onclose (en-US) 非标准

Is an event handler representing the code to be called when the close event is raised.

WorkerGlobalScope.onrejectionhandled 非标准

An event handler for handled Promise rejection events.

WorkerGlobalScope.onunhandledrejection 非标准

An event handler for unhandled Promise rejection events.

Methods

This interface inherits methods from the EventTarget interface and implements methods from WindowTimers, WindowBase64, WindowEventHandlers, and GlobalFetch.

Standard methods

WorkerGlobalScope.close() (en-US)

Discards any tasks queued in the WorkerGlobalScope's event loop, effectively closing this particular scope.

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() (en-US) 非标准

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.

Methods implemented from elsewhere

WindowBase64.atob()

Decodes a string of data which has been encoded using base-64 encoding.

WindowBase64.btoa()

Creates a base-64 encoded ASCII string from a string of binary data.

WindowTimers.clearInterval() (en-US)

Cancels the repeated execution set using WindowTimers.setInterval() (en-US).

WindowTimers.clearTimeout()

Cancels the repeated execution set using WindowTimers.setTimeout() (en-US).

ImageBitmapFactories.createImageBitmap() (en-US)

Accepts a variety of different image sources, and returns a Promise (en-US) which resolves to an ImageBitmap.

GlobalFetch.fetch()

Starts the process of fetching a resource.

WindowTimers.setInterval() (en-US)

Schedules the execution of a function each X milliseconds.

WindowTimers.setTimeout() (en-US)

Sets a delay for executing a function.

Example

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 (en-US). 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.

Specifications

Specification
HTML Standard
# the-workerglobalscope-common-interface

Browser compatibility

BCD tables only load in the browser

See also