Global JavaScript Object and Global Scope

  • What object in a web browser is a global object?
  • Is a global scope an object of a global object? If not, where is the global area found?
+7
javascript browser
source share
3 answers

In a browser environment, a window is considered a global area.

The window object implements the Window interface, which, in turn, inherits from the AbstractView interface.
Some additional global functions, namespace objects, interfaces, and constructors that are not usually associated with this window but are available on it are listed in the JavaScript Reference and DOM Reference.

The window object is the window itself.
The window document property points to the DOM document loaded into this window.
The window for this document can be obtained using the document.defaultView property.

In a tabbed browser, such as Firefox, each tab contains its own window object (and if you write the extension, the browser window is also a separate window.
That is, the window object is not shared between tabs in the same window. Some methods, namely window.resizeTo and window.resizeBy apply to the whole window, and not to the specific tab to which the window object belongs. As a rule, everything that cannot reasonably relate to a tab refers to a window.

+5
source share
  • the global object is called window
  • yes, the global scope is provided by the window, so you can get any global variable using window.varible
+3
source share

What a wonderful question. I thought about this for a while. Here are my thoughts. Yes, it’s true, something is called a global object and global reach. However, the global scope is virtual and literally reflects the mirror image of the global object (i.e., which properties present in the global object are present as variables in the global function / area). Any updates in the global area are updates to the global object (i.e., if you create a global variable, it is added to both the global area and the global object). Here's a good fact: when a global area is created or called, if you can, the context that is passed into it is actually a global object.

This is solid evidence that there is no code in JavaScript that actually runs outside the function. Some argue that top-level code and inline scripts are not actually executed inside a function, but this is not a true reason to create a global scope, a global function should be called, and that would mean that any top-level code would have to run inside this global function .

+1
source share

All Articles