Reputation: 271
I've been wanting to explore some of JavaScript's data structures lately and I realized I don't have an easy way to do it. Take, for instance, the following code:
var a="asdf";
The thing is, a
doesn't exist in its own space. It actually becomes a property of the window
object. This led to some questions, such as "Does anything own the window object?" and "is the console an object attached to the window?"
I figure that being able to see the parent object (assuming it exists) of a variable or object would be really nice. Is there a way to have the console tell you this?
Upvotes: 2
Views: 1317
Reputation: 707158
Each javascript environment has some sort of global object that "holds" the global variables and any other globally accessible things. In a browser javascript environment, this global object is the window
object. As you've discovered, all global variables in a browser are actually properties of the window
object. In other javascript environments such as server-side JS, the global object is not a window object, but there still is a global object that works similarly (though it doesn't have the same built-in browser window properties on it).
There is no generic way to query a variable and find out what its containing object is. And, in some cases (such as local variables in a function), there is no javascript accessible containing object that you can access the variables from.
The window
object itself is essentially owned or controlled by the browser. Each time the browser opens a new browser window or tab, it creates a new window object. It then loads a document into that window object for display and both the window
and document
objects are then accessible to the javascript in that page.
Upvotes: 2
Reputation: 71908
"Does anything own the window object?"
Yes, the window
object! :-) Try this:
window.window === window; // true
That's funny, but it doesn't mean that window
actually "owns" itself. It contains a reference to itself, called window
, and that's what makes the window
object globally available.
is the console an object attached to the window?
Yes.
I figure that being able to see the parent object (assuming it exists) of a variable or object would be really nice.
That's only possible on the global (window
) scope. But you can always create your own objects and use them as namespaces.
Upvotes: 1
Reputation: 22212
use namespace.
There are many articles about it. For example, here.
Define a namespace:
var AppSpace = AppSpace || {};
Then all the variables will be created under this namespace:
AppSpace.Podcast = function {
this.title = 'Astronomy Cast';
this.description = 'A fact-based journey through the galaxy.';
this.link = 'http://www.astronomycast.com';
};
AppSpace.Podcast.prototype.toString = function() {
return 'Title: ' + this.title;
}
Now those variable will not contaminate the window
object. It is usually a good practice to avoid possible name conflict if JS files included in a page happen to use the same variable name under window
.
Upvotes: -1