[英]A Few Questions About the Global Object
A global object is an object that always exists in the global scope. 全局对象是始终存在于全局范围中的对象。 In JavaScript there is always a global object defined. 在JavaScript中,始终定义一个全局对象。
Can there be more than one global object? 可以有多个全局对象吗? Why do people say Window is a global object when MDN doesnt say this? 当MDN不这么说时,为什么人们说Window是全局对象?
Every browser window has a single top level Window
object routinely referred to as global object . 每个浏览器窗口都有一个通常称为全局对象的顶级Window
对象 。 The global object is accessible via window
variable. 可以通过window
变量访问全局对象。
The object exposes a number of properties, most importantly document
which is also an object, methods, and events. 对象公开了许多属性,最重要的是document
,它也是对象,方法和事件。
A developer may define (via script) variables ( var
) and function
s which automatically become properties and methods of window
( global ) object (provided they are declared on top (global) level). 开发人员可以定义(通过脚本)变量( var
)和function
,这些变量和function
将自动成为window
( 全局 )对象的属性和方法(前提是它们在顶层(全局)级别上声明)。
For example, an expression var a = 1;
例如,表达式var a = 1;
declares a variable named a
(and assign numeric value to it). 声明一个名为a
的变量(并为其分配数值)。 This variable is accessible by its name ( a
) and as well as window.a
and window['a']
. 可通过其名称( a
)以及window.a
和window['a']
访问此变量。
The reference to the global window
object can be omitted so window.a = 2;
可以省略对全局window
对象的引用,因此window.a = 2;
and a = 2;
和a = 2;
are equivalent (but not ['a'] = 2;
which is syntax error). 是等效的(但不是['a'] = 2;
这是语法错误)。
Also note that global window
object belongs to the browser window, not to javascript. 另请注意, 全局window
对象属于浏览器窗口,而不是javascript。 The browser only expose it to Scripting Engine . 浏览器仅将其公开给脚本引擎 。
This is javascript
basics. 这是javascript
基础。
I hope this explanation clears a little your confusion. 我希望这个解释能消除您的困惑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.