Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is window really global in Javascript?

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> 
like image 915
Bart van Heukelom Avatar asked Jul 13 '11 13:07

Bart van Heukelom


People also ask

Is window a global object?

In a web browser, any code which the script doesn't specifically start up as a background task has a Window as its global object. This is the vast majority of JavaScript code on the Web.

Is window a global variable?

In HTML, the global scope is the window object. All global variables belong to the window object.

Is Windows a global scope?

The window global scope applies only to the main thread. In web workers there is no window global variable. Instead you have WorkerGlobalScope inside a WebWorker and in a SharedWorkerGlobalScope inside a SharedWorker .

Is global the same as window?

No difference. They both have the same effect (In the browser, where window is the global context1).


2 Answers

The reason why you can access "out of scope" or "free" variables in ECMAscript is the such called Scope chain. The scope chain is a special property from each Execution context. As mentioned several times before, a context object looks at least like:

  • [[scope]]
  • Variable / Activation Object
  • "this" context value

each time you access a variable(-name) within a context (a function for instance), the lookup process always starts in it's own Activation Object. All formal parameters, function declarations and locally defined variables (var) are stored in that special object. If the variablename was not found in that object, the search goes into the [[Scope]]-chain. Each time a function(-context) is initialized, it'll copy all parent context variable/activation objects into its internal [[Scope]] property. That is what we call, a lexical scope. That is the reason why Closures work in ECMAscript. Since the Global context also has an Variable Object (more precisely, **the variable object for the global object is the global object itself) it also gets copied into the functions [[Scope]] property.

That is the reason why you can access window from within any function :-)

The above explanation has one important conceptional conclusion: Any function in ECMAscript is a Closure, which is true. Since every function will at least copy the global context VO in its [[Scope]] property.

like image 91
jAndy Avatar answered Sep 23 '22 18:09

jAndy


Is window really global in Javascript?

Yes. Unless you create a new variable called window in a narrower scope

function foo() {     var window; } 

Inside foo we can access window, we all know that, but why exactly?

Any function can access variables declared in a wider scope. There is nothing special about window there.

like image 20
Quentin Avatar answered Sep 19 '22 18:09

Quentin