1

I need to define a function with a name that I have in the string.

Not a big deal, right?

window[fnName] = function(...args) {
  // ...
}

But what if I cannot access the window object? Like inside of Worker, for example.

Is it possible to do it without window and without eval()?

Ωmega
  • 42,614
  • 34
  • 134
  • 203
  • 1
    Try using `self` instead of `window` - https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/self – Mosh Feu Feb 12 '21 at 22:45
  • 1
    There is a new standard for that. globalThis is what youre searching for. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis – Kai Lehmann Feb 12 '21 at 22:48

1 Answers1

0

You can use self. It is property in both window and Worker scope that references back to their global scope. MDN/Window/self

Gryso
  • 208
  • 2
  • 6