Web Workers可以使用的函数和类

这篇翻译不完整。请帮忙从英语翻译这篇文章

除了标准的 JavaScript 函数集 (例如 String, Array, Object, JSON 等), DOM有多种功能可供 workers使用。

本文提供了一些列表。

Workers 在另一个全局上下文内运行, DedicatedWorkerGlobalScope 与当前 window不同。

默认情况下,Window的方法和属性不可用,但DedicatedWorkerGlobalScope像Window,实现WindowTimersWindowBase64

 

比较不同类型workers的属性和方法

Function Dedicated workers Shared workers Service workers Chrome workers Outside workers
atob() yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, on Window
btoa() yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, on Window
clearInterval() yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, on Window
clearTimeout() yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, on Window
dump() yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, on Window
setInterval() yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, on Window
setTimeout() yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, on Window
importScripts() yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, on WorkerGlobalScope no
close() yes, on WorkerGlobalScope yes, on WorkerGlobalScope yes, but is a no-op. Unknown no
postMessage() yes, on DedicatedWorkerGlobalScope no no Unknown no

APIs available in workers

Function Functionality Support in Gecko (Firefox) Support in IE Support in Blink (Chrome and Opera) Support in WebKit (Safari)
Broadcast Channel API Allows simple communication between browsing contexts (that is windows, tabs, frames, or iframes) with the same origin (usually pages from the same site). 38 (38) 未实现 未实现 未实现
Cache Cache API provides the ability to programmatically control cache storage associated with current origin. (Yes) 未实现 43 ?
Channel Messaging API Allows two separate scripts running in different browsing contexts attached to the same document (e.g., two IFrames, or the main document and an IFrame, two documents via a SharedWorker, or two workers) to communicate directly via two ports. 41 (41) (Yes) (Yes) (Yes)
Console API Provides access to the browser's debugging console (e.g., the Web Console in Firefox). The specifics of how it works vary from browser to browser, but there is a de facto set of features that are typically provided. 38 (38) (Yes) (Yes) (Yes)
CustomEvent The CustomEvent interface represents events initialized by an application for any purpose. 48 (48) (Yes) (Yes) (Yes)
Data Store A powerful, flexible storage mechanism for multiple Firefox OS applications to use to store and share data between one another quickly, efficiently, and securely. Only in Firefox OS internal (certified) applications, since v1.0.1. 未实现 未实现 未实现
DOMRequest and DOMCursor Respectively, these objects represents an ongoing operation (with listeners for reacting to the operation completely successfully, or failing, for example), and an ongoing operation over a list of results. 41 (41) ? ? ?
Fetch The Fetch spec provides an up-to-date definition of, and API for, fetching resources (e.g. across the network.) Mostly in 34 (34) behind pref, although a few features are later. 未实现 42
41 behind pref
未实现
FileReader This API allows asynchronous read of Blob and File objects. 46 (46) 未实现 (Yes) 未实现
FileReaderSync This API allows synchronous read of Blob and File objects. This is an API that works only in workers. 8 (8) 未实现 未实现 未实现
FormData FormData objects provide a way to easily construct a set of key/value pairs representing form fields and their values, which can then be easily sent using the XMLHttpRequest send() method. ? (should be in 39 (39)) ? (Yes) ?
ImageData The underlying pixel data of an area of a canvas element. Manipulating such data can be a complex task better suited to be delegated to a Web Worker. 25 (25) 未实现 未实现 未实现
IndexedDB Database to store records holding simple values and hierarchical objects. 37 (37),  42 (42) for IDBCursorWithValue. 10.0 (Yes) 未实现
Network Information API provides information about the system's connection in terms of general connection type (e.g., 'wifi', 'cellular', etc.). 53.0 (53) only on mobile (Yes) only on mobile 未实现 未实现
Notifications Allows web pages to control the display of system notifications to the end user 41 (41) ? ? ?
Performance The Performance interface represents timing-related performance information for the given page. 34.0 (34.0) ? 33.0 ?
Promise JavaScript objects that allow you to write asynchronous functions. 28 (28) (Yes) (Yes) (Yes)
Server-sent events Allows a server to push data to a web page at any point, after a connection has been opened to it. 53 (53) (currently only available in dedicated and shared workers; not service workers.) ? (Yes) ?
ServiceWorkerRegistration You can register a service worker from inside a standard worker, and use associated functionality. 40 (40) 未实现 (Yes) 未实现
TextEncoder and TextDecoder Create and return a new TextEncoder, or respectively TextDecoder, allowing to encode or decode strings into specific encodings. 20 (20) 未实现 未实现 未实现
URL Workers can use the static methods URL.createObjectURL and URL.revokeObjectURL with Blob objects accesible to the worker.
Workers can also create a new URL using the URL() constructor and call any normal method on the returned object.
21 (21) and 26 (26) for URL() constructor 未实现 未实现 未实现
WebGL with OffscreenCanvas WebGL (Web Graphics Library) is a JavaScript API for rendering interactive 3D and 2D graphics within any compatible web browser without the use of plug-ins. 44 (44) behind a feature preference setting. In about:config, set gfx.offscreencanvas.enabled to true. 未实现 未实现 未实现
WebSocket Creates and returns a new WebSocket  object; this mimics the behavior of the standard WebSocket() constructor. 37 (37) 11.0 (Yes) (Yes)
Worker Creates a new Worker. Yes, workers can spawn more workers. 3.5 (1.9.1) 10.0 未实现 See crbug.com/31666 未实现
WorkerGlobalScope The global scope of workers. This objects defines worker-specific functions. (Yes) 10.0 (Yes) (Yes)
WorkerLocation The subset of the Location interface available to workers. 3.6 (1.9.2) 10.0 (Yes) (Yes)
WorkerNavigator The subset of the Navigator interface available to workers. Basic implementation (Yes)
appCodeName, product, taintEnabled(): 28 (28)
onLine: 29 (29)
NavigatorLanguage: (Yes)
appName, appVersion, onLine, platform, userAgent: 10.0
Other: 未实现
(Yes) (Yes)
XMLHttpRequest Creates and returns a new XMLHttpRequest  object; this mimics the behavior of the standard XMLHttpRequest() constructor. Note that the responseXML and channel attributes on XMLHttpRequest always return null.

Basic: 3.5 (1.9.1)

response and responseType are available since 10 (10)

timeout and ontimeout are available since 13 (13)

(Yes) (Yes) (Yes)

See also

文档标签和贡献者

 此页面的贡献者: DoubleK2013, xgqfrms-GitHub
 最后编辑者: DoubleK2013,