对JavaScript中全局对象和作用域的一点点理解
在JavaScript中我们可以通过constructor来判断对象的类型。
如:arr.constructor == Array;
但不同的frame(或窗体)中判断时就会失效了,因为这时同样的对象有了不同的constructor。
开始不是很理解,认真学习了作用域后有了一些认识。
先看一个例子。
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2
<HTML>
3
<HEAD>
4
<TITLE> Scope of function </TITLE>
5
</HEAD>
6
<BODY>
7
<SCRIPT LANGUAGE="JavaScript">
8
function scope1(){
9
function User(){
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
}
12
this.user1 = new User();
13
}
14
function scope2(){
15
function User(){
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
}
18
this.user2 = new User();
19
}
20
var s1 = new scope1();
21
var s2 = new scope2();
22
alert(s1.user1.constructor == s2.user2.constructor);
23
</SCRIPT>
24
</BODY>
25
</HTML>
26![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
27![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
运行后弹出结果为false,也就是s1中的user1和s2中的user2的类型不同,但user1和user2都是User对象,且scope1和scope2中的User对象完全一样。
对于这种现象可以解释为两个User在不同的作用域下,所以他们的constructor是不同的。
从此可联想到在不同的frame或窗体中相同的对象的constructor不同,也是因为他们的作用域不同。
所以可以认为每个frame或窗体在运行时都会创建一个function,frame或窗体中的任何对象的作用域(即全局作用域)都在这个function中。
当然此处理解为创建一个function是由上面的例子联想到,实际上在每个frame或窗体创建时javascript解析器会初始化创建一个全局对象global object,这个全局对象拥有一些预定义的全局变量和全局方法,如Infinity, parseInt, Math,在此frame或窗体中定义的所有全局变量都是这个全局对象的属性。这个全局对象就是window。