2

Just out of curiosity, how would you access the global window object inside a function with this signature:

function bla(window){
   // How to access the global 'window' object here? Because 'window' is now local.
}
stackzebra
  • 450
  • 1
  • 6
  • 13
  • Can this help? https://stackoverflow.com/questions/33458255/passing-a-window-object-into-javascript-namespace – Deepak Dec 24 '21 at 16:49
  • Why shadow `window` like that? Also window is a reserved word in javascript and should never be used as a local – charlietfl Dec 24 '21 at 16:57
  • @charlietfl — It is not [a reserved word](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#reserved_keywords_as_of_ecmascript_2015) although shadowing it is a bad idea. This feels very much like a "Doctor! It hurts when I do this!" "So don't do that" problem. – Quentin Dec 24 '21 at 17:00

1 Answers1

4

You could use globalThis

a = 1;

function x(window) {
  console.log(window.a)
  console.log(globalThis.a)
}

x({ a: 2 })
skara9
  • 4,042
  • 1
  • 6
  • 21