///<class>继承Array类</class>
HotelInfoSet = function(){
  Array.apply(this);
}HotelInfoSet.prototype = new Array();HotelInfoSet.prototype.count = function(){
  return this.length;
}HotelInfoSet.prototype.add = function(hotelInfo){
  return this.push(hotelInfo);
}HotelInfoSet.prototype.AddRange = function(hotelInfoSet){
  this.concat(hotelInfoSet);
}var hotelInfoSet = new HotelInfoSet();
hotelInfoSet.add("aa");
hotelInfoSet.add("bb");
alert(hotelInfoSet.length);hotelInfoSet的长度为0,数据添加不到hotelInfoSet中,是不是继承Array有问题?

解决方案 »

  1.   


    <SCRIPT LANGUAGE="JavaScript">
    <!--
    HotelInfoSet = function(){ 
       this._strings = new Array();
    } //HotelInfoSet.prototype = new Array(); HotelInfoSet.prototype.count = function(){ 
      return this._strings.length; 
    } HotelInfoSet.prototype.add = function(hotelInfo){ 

      return this._strings.push(hotelInfo); 
    } HotelInfoSet.prototype.AddRange = function(hotelInfoSet){ 
      this.concat(hotelInfoSet); 
    } var hotelInfoSet = new HotelInfoSet(); 
    hotelInfoSet.add("aa"); 
    hotelInfoSet.add("bb"); 
    hotelInfoSet.add("bb");
    hotelInfoSet.add("bb"); 
    hotelInfoSet.add("bb"); 
    alert(hotelInfoSet.count()); 
    //-->
    </SCRIPT>
      

  2.   

    以下直接继承Array,为什么不可以?HotelInfoSet = function(){ 
      Array.apply(this); 
    } HotelInfoSet.prototype = new Array(); 
      

  3.   

    原型继承和构造继承都有局限性---不能实现核心对象继承
    举个例子你看:
    1.构造继承的局限性<SCRIPT LANGUAGE="JavaScript">
    <!--
    function MyDate(){
    this.base = Date;
    this.base.apply(this, arguments);
    }
    var date = new MyDate();
    alert(date.toGMTString)//返回undefined
    //-->
    </SCRIPT>
    2.原型继承的局限性<SCRIPT LANGUAGE="JavaScript">
    <!--
    function MyDate(){
    }
    MyDate.prototype = new Date();
    var date = new MyDate();
    alert(date.toGMTString)//显然也没达到效果//-->
    </SCRIPT>
    所以核心对象并不能像我们自定义对象那样工作,所以只能通过像我上面的实例继承达到效果。。
    不知道 我理解得是否正确
      

  4.   

    try this one/// <class>继承Array类 </class> 
    HotelInfoSet = function(){ 
      return Array.apply(this); 
    } HotelInfoSet.prototype = Array.prototype; HotelInfoSet.prototype.count = function(){ 
      return this.length; 
    } HotelInfoSet.prototype.add = function(hotelInfo){ 
      return this.push(hotelInfo); 
    } HotelInfoSet.prototype.AddRange = function(hotelInfoSet){ 
      this.concat(hotelInfoSet); 
    } var hotelInfoSet = new HotelInfoSet(); 
    hotelInfoSet.add("aa"); 
    hotelInfoSet.add("bb"); 
    alert(hotelInfoSet.length); 
    alert(hotelInfoSet[1]);
      

  5.   

    还不如返回一个array的实例的扩展。
      

  6.   

    HotelInfoSet = function(){ 
      this._arr = new Array();
    } HotelInfoSet.prototype.count = function(){ 
      return this._arr.length; 
    } HotelInfoSet.prototype.add = function(hotelInfo){ 
      return this._arr.push(hotelInfo); 

    var hotelInfoSet = new HotelInfoSet(); 
    hotelInfoSet.add("aa"); 
    hotelInfoSet.add("bb"); 
    alert(hotelInfoSet.count()); 
      

  7.   


    那样麻烦就是
    http://blog.csdn.net/muxrwc/archive/2007/10/25/1843775.aspx
    类似这样的。。