views:

49

answers:

3

Several friends suggest me to read developer.mozilla.org/en/JavaScript/Reference/Global_Objects:

The term "global objects"[1] here is not to be confused with the global object[2]. Here, global objects[3] refer to objects in the global scope. The global object[4] itself can be accessed by this in the global scope. In fact, the global scope consists of the properties of the global object[5] (including inherited properties, if any).

Honestly, I am completely confused by above words. The first sentence tells me not to be confused but it does confuse me. Well, English is not my mother language, maybe this is the reason. There appear 5 times of global object(s) and 3 times global scope!

Does global object[4,5] mean global objects[1] or global object[2]?

+1  A: 

Short answer: 4 and 5 are equivalent to 2.

Long answer: "global object" is a language construct of JavaScript and can be accessed using the global or window variables in your code. This object contains everything that is somehow defined.

"global objects" means all objects that are defined in the main scope of a JavaScript environment. The main scope (or "global scope") means immediate children (properties) of the "global object".

If i define a variable myVariable in JavaScript outside of any function, this variable is in the "global scope" i.e. accessible by using global.myVariable (or window.myVariable) where global is a reference to the "global object".

I recommend taking a look at how JavaScript's function scope works: https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope

elusive
`this` has nothing to do with scope.
Tim Down
@Tim Down: Right. Edited my post accordingly.
elusive
+1  A: 

Mozilla's wording is unhelpful. I suggest you ignore it.

There is one global object. This is a well-defined construct within the ECMAScript language specification. It has several purposes, not least of which is that properties of the global object are available everywhere. This is what Mozilla means by "Global Objects"; they're more accurately and helpfully called "properties of the global object".

What the article means by global scope is code that is not inside any function. In such code, this is a reference to the global object. In JavaScript within browsers, window can be thought of as being the global object and is accessible everywhere.

In summary, 2, 4 and 5 are the same thing. 1 and 3 are a poor name for "properties of the global object" that you should forget about.

Tim Down
A: 

The Global object is the main object. Think of it as God. Like God, there is only one Global object. Inside the browsers, the window identifier refers to it.

Global objects are all the objects that are properties of God (the Global object). The browsers have lots of global objects built-in, like location, document, XMLHttpRequest, the alert method, etc.

You can create additional global objects by:

  • declaring global (non-primitive) variables (declaring variables in the global execution context)
  • declaring global functions (declaring functions in the global execution context)

It is probably best to refer to global objects as "global variables", "global functions", "global members", "global properties", etc. so that they are not confused with the Global object.

Šime Vidas