/** * 定义一个基础的类对象,用于判定类是否相等。 * 使用方法: * var obj=new BaseObject(1); * var obj2=new BaseObject(1); * alert(obj.equals(obj2)); * @returns {BaseObject} */ function BaseObject() { var id=arguments[0]; this.id=""; if(id!=undefined) this.id=id; } /** * 给BaseObject扩展一个equals方法。 * @param obj * @returns {Boolean} */ BaseObject.prototype.equals=function(obj){ return this.id==obj.id; } /** * 扩展一个类继承BaseObject。 * 增加:title和url两个属性。 */ function DataRow(){ this.title=""; this.url=""; var id=arguments[0]; //调用构造函数 if(id!=undefined) BaseObject.call(this , id); var title=arguments[1]; var url=arguments[2]; if(title!=undefined) this.title=title; if(url!=undefined) this.url=url; } //建一个积累的对象作为子类原型的原型(原型继承) //DataRow继承BaseObject DataRow.prototype=new BaseObject(); /** * 创建一个DataRowHelper类。 * 定义: * add,remove,length,each四个方法。 * @returns */ var DataRowHelper= function(){ this.RowCollection=new Array(); }; //添加 DataRowHelper.prototype.add=function(row){ var isExist=false; for(var i=0;i=0;i--) { var obj=this.RowCollection[i]; if(row.equals(obj)) { this.RowCollection.splice(i,1); break; } } } DataRowHelper.prototype.length=function(){ return this.RowCollection.length; } //对象遍历 DataRowHelper.prototype.each=function(func){ var len = this.RowCollection.length; for(var i=0;i