<script>
function Test()
{
this.name1 ="11" ;
this.name2 ;
this.name3 =11 ;
this.name4 ={name:11} ;
this.name ={obj:123} ;
this.showName = function()
{
/*
问题1:this.name1 、this.name2、this.name3、this.name4 为什么是undefined;
问题2:this.name 为什么是 string 类型的,无论我给这个声明赋值的是数字,对象,字符串,还是不赋值,结果都是string 类型
求解答..!
*/
////////////////////////////////////////////////////////////////////////////////////////
alert(" name type :"+typeof(this.name))
alert(" name1 type :"+typeof(this.name1));
alert(" name2 type :"+typeof(this.name2));
alert(" name3 type :"+typeof(this.name3));
alert(" name4 type :"+typeof(this.name4));
////////////////////////////////////////////////////////////////////////////////////////
var string = "";
var num = 123;
var obj ={};
var undi ;
alert(" string type :"+typeof(name));
alert(" num type :"+typeof(num));
alert(" obj type :"+typeof(obj));
alert(" undi type :"+typeof(undi));
////////////////////////////////////////////////////////////////////////////////////////
}
setTimeout(this.showName,1000);
}
var test = new Test(); </script>
function Test()
{
this.name1 ="11" ;
this.name2 ;
this.name3 =11 ;
this.name4 ={name:11} ;
this.name ={obj:123} ;
this.showName = function()
{
/*
问题1:this.name1 、this.name2、this.name3、this.name4 为什么是undefined;
问题2:this.name 为什么是 string 类型的,无论我给这个声明赋值的是数字,对象,字符串,还是不赋值,结果都是string 类型
求解答..!
*/
////////////////////////////////////////////////////////////////////////////////////////
alert(" name type :"+typeof(this.name))
alert(" name1 type :"+typeof(this.name1));
alert(" name2 type :"+typeof(this.name2));
alert(" name3 type :"+typeof(this.name3));
alert(" name4 type :"+typeof(this.name4));
////////////////////////////////////////////////////////////////////////////////////////
var string = "";
var num = 123;
var obj ={};
var undi ;
alert(" string type :"+typeof(name));
alert(" num type :"+typeof(num));
alert(" obj type :"+typeof(obj));
alert(" undi type :"+typeof(undi));
////////////////////////////////////////////////////////////////////////////////////////
}
setTimeout(this.showName,1000);
}
var test = new Test(); </script>
{
this.name1 ="11";
this.name2;
this.name3 =11;
this.name4 ={name:11};
this.name ={obj:123};
this.showName = function(){};
}
var test = new Test();
alert(" name type :"+typeof(test.name))
alert(" name1 type :"+typeof(test.name1));
alert(" name2 type :"+typeof(test.name2));
alert(" name3 type :"+typeof(test.name3));
alert(" name4 type :"+typeof(test.name4));
js作用域是function(){}函数划分的~·楼主具体网上查查~·js作用域
那么为什么,this.name 却是string 类型,按照上楼的说法,this.name的值也应该是 undefined 的呀
难道它是个类似与关键字的默认属性????
在setTimeout的function里用this关键字,其实指代的并不是Test()类本身,而是window对象,楼主可以测试一下,看看是不是,window对象的name属性是空的string类型。
问题1:因为this指代window对象,所以this.name1 、this.name2、this.name3、this.name4显示undefined
问题2:因为this.name实际上指代的是window的name属性,所以不管你怎么改变Test类中的this.name项,输出类型还是string。
alert(this.name);
alert(obj.name);
这里涉及到一个闭包的问题
http://www.cn-cuckoo.com/2007/08/01/understand-javascript-closures-72.html#clResO
楼主可以去看看
我刚刚把 window的这个name属性 指定一个值,结果在setTimeout() 里面打印的就是 就是 window 指定的值,明白的很透彻!
感谢,灰常感谢!!!! 结贴!
都结贴了就不多说了。呵呵~