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