完整代码

ZDNet软件频道 时间:2002-08-07 作者: |  我要评论()
本文关键词:
function Item(pvThing) {
      this.Value = pvThing;
      this.Index = -1;
}

function Collection() {
      this.all = [];
      this.aall = {};
      this.length = 0;
  this.Add = function (poItem, pvKey) {
            if (typeof(poItem) == "object" && poItem.constructor == Item) {
                  var idx = this.all.push(poItem) - 1; // 'push' was added in IE 5.5
                  if (this.all[idx].Index = -1) {
                        this.all[idx].Index = idx;
                  }
                  if (typeof(pvKey) != "undefined") {
                        this.all[idx].Key = pvKey;
                        this.aall[pvKey] = this.all[idx];
                  }
                  this.length = this.all.length;
            } else {
                  alert("Item must be an Item object.");
            }
      }
 this.Remove = function (pvItem) {
            if (typeof(pvItem) == "object" && pvItem.constructor == Item) {
                  for (var i = 0; i < this.all.length; i++) {
                        if (this.all[i] == pvItem) {
                              this.all.splice(i, 1);
                              if (typeof(this.all[i].Key) != "undefined") {
                                    delete this.aall[this.all[i].Key] ;
                              }
                              break;
                        }
                  }
            } else if (typeof(this.aall[pvItem]) != "undefined") {
                  this.all.splice(this.aall[pvItem].Index, 1); // 'splice'
was added in IE 5.5
                  delete this.aall[pvItem];
            } else if (typeof(this.all[pvItem]) != "undefined") {
                  if (typeof(this.all[pvItem].Key) != "undefined") {
                        delete this.aall[this.all[pvItem].Key];
                  }
     this.all.splice(pvItem, 1);
            }
            for (var i = 0; i < this.all.length; i++) {
                  this.all[i].Index = i;
            }
            this.length = this.all.length;
      }

   this.Item = function (pvKeyOrIndex) {
            if (typeof(this.aall[pvKeyOrIndex]) != "undefined") {
                  return this.aall[pvKeyOrIndex];
            } else if (typeof(this.all[pvKeyOrIndex]) != "undefined") {
                  return this.all[pvKeyOrIndex];
            }
      }

      this.RemoveAll = function () {
            this.all = [];
            this.aall = {};
            this.length = 0;
      }

}

//push and splice do not work in versions of IE less than 5.5.
//you can create new functions to do this functionality using
//combinations of concat and slice.


百度大联盟认证黄金会员Copyright© 1997- CNET Networks 版权所有。 ZDNet 是CNET Networks公司注册服务商标。
中华人民共和国电信与信息服务业务经营许可证编号:京ICP证010391号 京ICP备09041801号-159
京公网安备:1101082134