Take this piece of Javascript in a browser: <script> console.log(window.someThing); var x = 12; function foo() { window.otherThing = x; } </script>Inside `foo` we can access `window`, we all know that, but why exactly?- Is it some kind of special global variable?- Or does the "root scope" (inside the `script` tag) have it as an implicit local variable and is it simply "closure-inherited" as any other local variable (like `x` above) can be?And how does that concur with variables declared directly inside the `script` tag being set as properties of `window`? (Or is that not so?) <script> var x = 12; function() { console.log(window.x); } </script>
The reason why we can access window inside of the function is because window is representing the 'window / DOM' where the javascript is being compiled in and it is injected into the javascript code. It is part of the interface of the javascript console. So when you store a value to the property of window you are simply storing it in the global access of the the script in which the code is running it. Think of it as being a cloud storage for where there is a window containing the DOM document.