日期:2014-05-16  浏览次数:20376 次

帮我看看这代码是什么问题
<script   type= "text/javascript ">
function   NewObj()
{
      this.a=new   Array();
      this.Items=function()
      {
            return   this.a;
      }
      this.b=function(name)
      {
            if(name== " ")
                  return;
            this.a.push(name);
      }
}
function   test()
{
      var   tObj=new   NewObj();
      tObj.b( "c ");
      alert( "Items.length= "+tObj.Items.length+ "   a.length= "+tObj.a.length9:25   2006-7-13);
}
</script>
<input   type= "button "   value= "test "   onclick= "test() ">

为何Items.length与a.length的值不相同???

------解决方案--------------------
a.length是数组的长度,Items.length是一个函数体,长度为一,加一个()表示调用该函数并得到返回值,如下:

alert( "Items.length= "+tObj.Items.length+ " a.length= "+tObj.a.length);
====>
alert( "Items.length= "+tObj.Items().length+ " a.length= "+tObj.a.length);
------解决方案--------------------
<script type= "text/javascript ">
function NewObj()
{
this.a=new Array();
this.Items=function(a)
{
return this.a;
}
this.b=function(name)
{
if(name== " ")
return;
this.a.push(name);
}
}
function test()
{
var tObj=new NewObj();
tObj.b( "c ");
alert( "Items.length= "+tObj.Items.length+ " a.length= "+tObj.a.length);
}
</script>
<input type= "button " value= "test " onclick= "test() ">
-----------------------------------
这样就行了,上面加上a