<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> 

解决方案 »

  1.   

    function Test()  
    {  
      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)); 
      

  2.   


    js作用域是function(){}函数划分的~·楼主具体网上查查~·js作用域
      

  3.   

    按照函数划分的话, 那么this.name1 ,this.name2,this.name3,this.name4 值为undefined说的通,
    那么为什么,this.name 却是string 类型,按照上楼的说法,this.name的值也应该是  undefined 的呀
    难道它是个类似与关键字的默认属性????
      

  4.   

    setTimeout是window对象封装的方法,一般用的时候会这么调,window.setTimeout(function,time),
    在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。
      

  5.   

    var obj = this;
    alert(this.name);
      

  6.   

    错了
    alert(obj.name);
    这里涉及到一个闭包的问题
      

  7.   

    至于JS的作用域的概念,推荐个网址:
    http://www.cn-cuckoo.com/2007/08/01/understand-javascript-closures-72.html#clResO
    楼主可以去看看
      

  8.   

    哦,这个样子啊..明白了..原来这个this  是代表的着 window 对象的呀,那这一切,我都弄明白了,
    我刚刚把 window的这个name属性 指定一个值,结果在setTimeout() 里面打印的就是 就是 window 指定的值,明白的很透彻!
     
    感谢,灰常感谢!!!! 结贴!
      

  9.   

    楼主似乎并没弄明白啊。这里的this指向的并不是window,严格的说不是直接指向,而是通过原型链。
    都结贴了就不多说了。呵呵~