8

When we define a top level named function in Javascript, it becomes a method of the window object. What is the reason for this design decision?

1

2 Answers 2

4

Javascript always run in a particular global context. In a browser environment that is the window object, so when you define a global, you're actually adding it to window...

Here is another excellent stackoverflow answer explaining why this is so: Is window really global in Javascript?

8
  • This is true, but does not explain why window was chosen instead of a dedicated global object. To be fair though, I don't know if this answer is easy to find if you haven't been in involved in browser / JS engine development May 27, 2013 at 19:25
  • @FelixKling what would you suggest be the topmost context then? :)
    – Dory Zidon
    May 27, 2013 at 19:51
  • As I said, you could simply have another dedicated global object, accessible as global, like in Node.js. That a global has to exist is clear, but why is it window? May 27, 2013 at 21:03
  • @FelixKling: The concept is the same in NodeJS. The global is just exposed under a different name, which is expected since the name window wouldn't make much sense on a server. In other words, window in a browser, and global in NodeJS is the dedicated global object.
    – user1106925
    May 27, 2013 at 21:36
  • @squint: I know. But window contains other properties as well, which are not part of the ES specification. I know that the spec explicitly allows this, but since window already contains many DOM related properties, using a dedicated global object for global variables only, instead of such a mix up, doesn't seem to be a bad solution. That is what I meant. May 27, 2013 at 21:52
3

What is the reason for this design decision?

I can't answer that, we'd have to ask the guy who designed the language, and the committee that maintains its specification. But maybe this will give you some insights: internally, variables (local or global) are always stored in "Lexical Environment" objects. So, they are always properties, and they even have their own properties (to mark them as read-only, non-deletable, etc.).

The environment objects are not exposed in browser JavaScript, except for the global object (as pointed out by squint in the comments, ECMAScript doesn't actually require that the global variable object be exposed, that's up to the implementation.)

It happens that it was decided that the global object is the same as the window object in browsers. Why? Again, I can't answer that. They just decided not to use a dedicated global (or whatever) identifier.

6
  • 3
    +1 I'd only add the small point that ECMAScript doesn't actually require that the global variable object be exposed. An implementation could decide to not expose it, and it would be compliant.
    – user1106925
    May 27, 2013 at 20:06
  • 1
    Thanks, added a note about that. But let me ask: in implementations where it's not exposed (if you know any), is it still be possible to access stuff like parseInt and the Object constructor?
    – bfavaretto
    May 27, 2013 at 20:28
  • 3
    Since parseInt and Object are properties of the global object and therefore global variables, they can still be accessed. Even if the object itself is not exposed, those properties/variables will be looked up through the scope chain. I have to say though that I don't know such an implementation, this is my interpretation from the specification. May 27, 2013 at 21:06
  • 1
    Yeah, what @FelixKling said. :-) If you're curious, the last paragraph of 15.1 is where the window property is described as an example of a "host" property.
    – user1106925
    May 27, 2013 at 21:29
  • @squint So that's the reason for window.window.window.window.... Without it, window itself wouldn't be exposed!
    – bfavaretto
    May 27, 2013 at 22:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.