采用Array.prototype.newFunction=xxx的办法不好,会影响所有的Array对象我想创建一个全新的类,以Array为基类,可以避免影响全局。但是我用下列办法时,结果错误。function CA(){
  this.count=function(){
    return this.length;
  }
}
CA.prototype=new Array();
var o=new CA();
o.push(1);
o.push(2);
o[3]=3;
alert("o.count()=" + o.count() + "\no.length=" + o.length);以上运行无错,单显示结果均为为0问题出在哪里??

解决方案 »

  1.   

    ECMAScript规定了宿主类和本地类是不允许作为继承基类
      

  2.   

    你创建一个扩展类是不可行了,基于对象来扩展吧var o = new Array();
    o.count = function() {
    return i.length;
    }
    o.push(1);
    o.push(2);
    o[3]=3;
    alert("o.count()=" + o.count() + "\no.length=" + o.length); 其中o[3]代表第4个元素
    这里o[0]=1
    o[1]=2
    o[2]=undefined;
    o[3]=3;
      

  3.   

    修正
    var o = new Array();
    o.count = function() {
    return this.length;
    }
    o.push(1);
    o.push(2);
    o[3]=3;
    alert(o[2]);
    alert("o.count()=" + o.count() + "\no.length=" + o.length);