简体   繁体   中英

JavaScript global object and the global scope

  1. Which object in the web browser is the global object?
  2. Is the global scope the scope provided by the global object? If not, then where is the global scope found?

In a browser enviroment, the Window is considered the global scope.

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

The window object represents the window itself.
The document property of a window points to the DOM document loaded in that window.
A window for a given 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're writing an extension, the browser window itself is a separate window too.
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 the window object belongs to. Generally, anything that can't reasonably pertain to a tab pertains to the window instead.

  1. the global object is called window
  2. yes , the global scope is provided by window, so u can get any global variable with window.varible

What a wonderful question. I have been pondering about this for a while myself. Here are my thoughts. Yes its true there is something called a global object and global scope. However, the global scope is virtual and literally a live mirror reflection of the global object(ie what ever properties present in the global object are present as variables in the global function/scope). Any updates on the global scope is an update the global object(ie if you create a global variable, it is added to both the global scope and global object). Here is the cool fact: when the global scope is created or invoked if you may, the context that gets passed into it is in fact the global object.

This is hardcore proof that there is no code that actually executes outside of a function in JavaScript. Some argue that top level code and inline scripts don't actually execute inside a function, but thats not true cause for the global scope be created, a global function has to be be invoked and that would mean any top level code would have to run inside that global function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM