title: Reliably detect if the script is executing in a web worker
body: <p>I am currently writing a little library in JavaScript to help me delegate to a web-worker some heavy computation .</p>
  
  <p>For some reasons (mainly for the ability to debug in the UI thread and then run the same code in a worker) I'd like to detect if the script is currently running in a worker or in the UI thread.</p>
  
  <p>I'm not a seasoned JavaScript developper and I would like to ensure that the following function will reliably detect if I'm in a worker or not :</p>
  
  <pre><code>function testenv() {
      try{
          if (importScripts) {
              postMessage("I think I'm in a worker actually.");
          }
      } catch (e) {
          if (e instanceof ReferenceError) {
              console.log("I'm the UI thread.");
          } else {
              throw e;
          }
      }
  }
  </code></pre>
  
  <p>So, does it ?</p>