当前位置: 首页 > 图文教程 > 网络编程 > Javascript > Javascript 陷阱 window全局对象

Javascript
Add a Table to a Word Document
Add Formatted Text to a Word Document
用jscript实现新建word文档
用jscript实现新建和保存一个word文档
Open and Print a Word Document
Use Word to Search for Files
Convert Seconds To Hours
Sample script that deletes a SQL Server database
Sample script that displays all of the users in a given SQL Server DB
firefox中用javascript实现鼠标位置的定位
div+css实现鼠标放上去,背景跟图片都会变化。
Locate a File Using a File Open Dialog Box
Save a File Using a File Save Dialog Box
用jscript实现列出安装的软件列表
List the Stored Procedures in a SQL Server database
Display SQL Server Login Mode
Display SQL Server Version Information
List all the Databases on a SQL Server
用jscript启动sqlserver
Stop SQL Server

Javascript 陷阱 window全局对象


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 140 ::
收藏到网摘: n/a

ff和ie6下都是同样的运行结果,看来不是bug,那究竟是为什么呢? 注意到:对象方法访问其对象的属性时|必须|加以this.(和java不一样). function Test(){
this.name='Test';
var name=2;
this.show=function(){
alert(name);
alert(this.name);//显示名字
}
}
var test=new Test();//通过构造器创建一个对象
test.show();//输出2和'Test' ,说明对象方法访问其属性时必须加this.
function Test2(){
this.name='Test2';
this.show=function(){
alert(name);
alert(this.name);
}
}
Test();//直接调用Test();
var test2=new Test2();
test2.show();//输出了Test,Test2,很奇怪啊,name问什么有值了,而且怎么会是'Test',bug?
alert(name);
window.show(); //输出2,test;怎么会有show函数呢,难道是bug
//依次输出2,Test;Test,Test2;Test;2,Test
// ff和ie6下都是同样的运行结果,看来不是bug,那究竟是为什么呢?
// 注意到:对象方法访问其对象的属性时|必须|加以this.(和java不一样).
// 整个页面是默认的是|window|对象,那么定义的函数,默认就是window对象的方法。
//直接直接调用函数时,相当于通过window.调用方法,那么方法内部的this自然就是\
//window对象,this.name='Test'就是为window对象加了一个属性.
// 那么当方法局部作用域内未定义name且调用alert(name),就相当于调用alert(window.name);
/*啊,代码混乱了。这看起来像一个很严重的陷阱!!*/