最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What does the 'self' keyword mean in WebWorkers - Stack Overflow

programmeradmin2浏览0评论

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

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

worker.addEventListener('message', function(e) {
  console.log('Worker said: ', e.data);   // Here it send's 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 is doing this block of code? 1.what code line fires the message event in line 7? 2.what data is passed in the postMessage on line 8? 3.what does self do here?

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

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

worker.addEventListener('message', function(e) {
  console.log('Worker said: ', e.data);   // Here it send's 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 is doing this block of code? 1.what code line fires the message event in line 7? 2.what data is passed in the postMessage on line 8? 3.what does self do here?

Share Improve this question edited Jun 9, 2015 at 15:12 taxicala 21.8k7 gold badges40 silver badges66 bronze badges asked Jun 9, 2015 at 14:54 Alanas MacijauskasAlanas Macijauskas 2635 silver badges21 bronze badges 8
  • "Self" probably refers to an object (the "this"), however, it's probably in the context of an anonymous function, so they might have used "self" instead of the .bind() method. Can you post the rest of the code please? There's plenty more, I'm sure. – Josh Beam Commented Jun 9, 2015 at 14:58
  • It's a reference to a global object in web workers, since they have no browsing context and no window object. – Teemu Commented Jun 9, 2015 at 15:02
  • @JoshBeam I myself thought the same thing, it turns out it's a whole other world. See html5rocks./en/tutorials/workers/basics – php_nub_qq Commented Jun 9, 2015 at 15:03
  • 1 I don't understand why so many downvotes. It's a good question. – taxicala Commented Jun 9, 2015 at 15:03
  • @php_nub_qq, interesting, thanks for pointing that out! I retracted my close vote, I'm curious to see the answer to this question. – Josh Beam Commented Jun 9, 2015 at 15:07
 |  Show 3 more ments

1 Answer 1

Reset to default 6

the keyword self is used to approach the Worker’s API, which means that no matter the scope (even if its a closure) you will get access to the Worker's API (I'm not sure if you can redeclare self to something else and loose the Worker's API reference, but I believe it is protected by JS so you don't be able to override it).

And the following lines:

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

Are simply adding an event listener for the 'message' event, which will send the data from the event back to the reference of the webworker in the context where it was spawned (most monly the current browsers thread, or a parent worker). And we can quote what the false boolean determines:

useCapture Optional If true, useCapture indicates that the user wishes to initiate capture. After initiating capture, all events of the specified type will be dispatched to the registered listener before being dispatched to any EventTarget beneath it in the DOM tree. Events which are bubbling upward through the tree will not trigger a listener designated to use capture. See DOM Level 3 Events and JavaScript Event order for a detailed explanation. If not specified, useCapture defaults to false.

Note: For event listeners attached to the event target; the event is in the target phase, rather than capturing and bubbling phases. Events in the target phase will trigger all listeners on an element regardless of the useCapture parameter.

Note: useCapture became optional only in more recent versions of the major browsers; for example, it was not optional prior to Firefox 6. You should provide this parameter for broadest patibility.

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

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

A more technical description of the keyword self:

The self read-only property of the WorkerGlobalScope interface returns a reference to the WorkerGlobalScope itself. Most of the time it is a specific scope like DedicatedWorkerGlobalScope, SharedWorkerGlobalScope, or ServiceWorkerGlobalScope.

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

发布评论

评论列表(0)

  1. 暂无评论