This is quite unclear to me, the definition of global object seem to be “The global object provides variables and functions that are available anywhere”.
Now this surely means that the global variable environment contains variables available everywhere in the other scopes, but does this also mean that, for example, the window object is at the higher end of the scope chain? As in JS everything is an object, is this true for the prototype chain too?
Because from my notes Object.prototype looks to be at the top of the prototype chain.
In fact if i do:
Object.getPrototypeOf(window) i get WindowPrototype { … }
but if i do:
Object.getPrototypeOf(Object.prototype) i get null
Another thing unclear to me is what is the difference between window (lowercase) and Window (uppercase)… I know that uppercases are usually used for constructors, is this the case too? Is Window a constructor and window an object? If so, what’s the purpuose of the Window constructor?
These are similar but distinct concepts. Most languages have a concept of (lexical) scope, which is how variables are resolved to their values. For instance,
var a = 10; function foo() { return a; }Here,
ais in the global scope and it’s referenced by the closurefoo. Any changes made toa(so long as it isn’t shadowed) will also appear in theathat can be seen insidefoo.JS is also what’s called a prototype-based language, so it has an additional more exotic concept called the “prototype chain”. This is how it implements something approximating OOP inheritance - essentially, if I request
obj.x, JS will first check ifobjhas thexproperty, and if it doesn’t find it, it follows the__proto__(note: nonstandard) value to get the prototype of that object and checks there. If that prototype doesn’t havex, the process continues until it reaches the end of the chain.The JS global object (also called
windowin the browser - which has a reference to itself calledwindow) is part of lexical scoping, but not the prototype chain. The global object is the end of the scope chain, but the end of the prototype chain isObject.getPrototypeOf(Object.prototype) == null(note: we need to get the prototype ofObject.prototypebecauseObjectis the constructor function, not the prototype)Windowis the constructor of thewindowobject,Object.getPrototypeOf(window) == Window.prototypeandwindow.constructor == Window. It’s a weird quirk of Javascript, “classes” are actually the constructor, and when we prototype a new object it prototypes that function’sprototypeproperty.Thank you so much! Now it’s definetely clearer
You’re confusing scope and inheritance.



