Search code examples
javascriptweb-worker

What equivalant to window[funcName]() in web-worker JS?


How can we write window[funcName]() in web-worker JS?

how access to function declared in top context of script ?


Solution

  • Inside a WebWorker you can use self to reference to the global scope (you could also use this (when it points to the global scope, exactly like it works for window):

    // worker.js
    function foo() {
      console.log("hello worker");
    }
    
    self['foo']()