JavaScripture
Contribute via GitHub Feedback

WorkerGlobalScope : Global

The following are properties of the global object when running inside a Worker background process. They can be accessed from anywhere without additional qualifiers.

Instance Properties

globalThis : WorkerGlobalScope  

Returns the global object itself. This is the same as self (in a Worker context).

Example:

Run

Results:

 

location : Location  

Returns the location of the worker script.

Example:

Run

Results:

 

self : WorkerGlobalScope  

Returns the global object itself. This is the same as globalThis (in a Worker context).

Example:

Run

Results:

 

Instance Methods

addEventListener 2 variants
addEventListener(type : String, listener : Function, [useCapture : Boolean]) : undefined
listener(event : Event) : undefined
addEventListener(type : String, listener : EventListener, [useCapture : Boolean]) : undefined
close() : undefined

Stops the worker.

Example:

Run

Results:

 

dispatchEvent(event : Event) : Boolean
postMessage(message : Object, [transfer : Array]) : undefined

Sends a message to the main thread. The main thread receives the message through the onmessage event. message will be in the event.data property.

Example:

Run

Results:

 

removeEventListener 2 variants
removeEventListener(type : String, listener : Function, [useCapture : Boolean]) : undefined
listener(event : Event) : undefined
removeEventListener(type : String, listener : EventListener, [useCapture : Boolean]) : undefined
reportError(error : Object) : undefined

Fires the error event with the specified error. Unlike throw error, this will always cause the error event to fire, even if there is a catch handler around the call.

Example:

Run

Results:

 

Instance Events

onerror / 'error'  
listener(error : String, source : String, line : Number, column : Number) : undefined

Fired when there is an error in the worker. See also reportError().

Example:

Run

Results:

 

onmessage / 'message'  
listener(event : Event) : undefined

Called when Worker.postMessage() is called. event.data contains the message passed to postMessage().

Example:

Run

Results:

 

onoffline / 'offline'  
listener(event : Event) : undefined

Fired when the browser loses network connection.

ononline / 'online'  
listener(event : Event) : undefined

Fired when the browser gains network connection.