What does the "i" keyword mean in WebWorkers

I don't understand line 7,8,9:

var worker = new Worker('doWork.js');

worker.addEventListener('message', function(e) {
  console.log('Worker said: ', e.data);   // Here it send the data.
}, false);

worker.postMessage('Hello World'); // Send data to our worker.



 //7 self.addEventListener('message', function(e) {
   //8   self.postMessage(e.data);
   //9 }, false);

      

What does this block of code do? 1. Which line of code triggers the message event on line 7? 2. What data is passed to postMessage on line 8? 3. What makes itself here?

+3


source to share


1 answer


the keyword is self

used to approach the Workers API, which means that regardless of the scope (even if it is closed), you will have access to the Worker API (I'm not sure if you can update self

anything else and lose reference to Worker API, but I believe it is protected by JS so you won't be able to override it).

And the following lines:

self.addEventListener('message', function(e) {
    self.postMessage(e.data);
}, false);

      

Just add an event listener for the event 'message'

that will send the data from the event back to the web worker link in the context where it was spawned (most often the current browser thread or parent worker). And we can quote what the boolean expression defines false

:

useCapture

Optional If true, useCapture indicates that the user wishes to initiate a capture. Once capturing begins, all events of the specified type will be sent to the registered listener before dispatched to any EventTarget below it in the DOM tree. Activities that bubble up through the tree will not trigger the listener to use the capture. See DOM Level 3 Events and JavaScript Event Ordering for a detailed explanation. If not specified, useCapture defaults to false.

Note. For event listeners associated with the event, the target; the event is in target phase, not bubble phases. Events in the target stage will call all listeners on the element regardless of the useCapture parameter.

     

Note: useCapture has become optional only in later versions of major browsers; for example, it was not optional until Firefox 6. You must provide this parameter for maximum compatibility.

wantsUntrusted

If true, the listener will receive synthetic events dispatched by web content (default is false for chrome and true for regular web pages). This option is only available in Gecko and is mostly useful for code in add-ons and in the browser itself. See Interaction between privileged and non-privileged pages for example.



From: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

A more detailed description of the keyword self

:

Returns the Self read-only property of the WorkerGlobalScope interface a reference to the WorkerGlobalScope itself. In most cases, this is something like DedicatedWorkerGlobalScope, SharedWorkerGlobalScope, or ServiceWorkerGlobalScope.

Quoting from: https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/self

+5


source







All Articles