ObjectUtil.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * 定义一个基础的类对象,用于判定类是否相等。
  3. * 使用方法:
  4. * var obj=new BaseObject(1);
  5. * var obj2=new BaseObject(1);
  6. * alert(obj.equals(obj2));
  7. * @returns {BaseObject}
  8. */
  9. function BaseObject()
  10. {
  11. var id=arguments[0];
  12. this.id="";
  13. if(id!=undefined)
  14. this.id=id;
  15. }
  16. /**
  17. * 给BaseObject扩展一个equals方法。
  18. * @param obj
  19. * @returns {Boolean}
  20. */
  21. BaseObject.prototype.equals=function(obj){
  22. return this.id==obj.id;
  23. }
  24. /**
  25. * 扩展一个类继承BaseObject。
  26. * 增加:title和url两个属性。
  27. */
  28. function DataRow(){
  29. this.title="";
  30. this.url="";
  31. var id=arguments[0];
  32. //调用构造函数
  33. if(id!=undefined)
  34. BaseObject.call(this , id);
  35. var title=arguments[1];
  36. var url=arguments[2];
  37. if(title!=undefined)
  38. this.title=title;
  39. if(url!=undefined)
  40. this.url=url;
  41. }
  42. //建一个积累的对象作为子类原型的原型(原型继承)
  43. //DataRow继承BaseObject
  44. DataRow.prototype=new BaseObject();
  45. /**
  46. * 创建一个DataRowHelper类。
  47. * 定义:
  48. * add,remove,length,each四个方法。
  49. * @returns
  50. */
  51. var DataRowHelper= function(){
  52. this.RowCollection=new Array();
  53. };
  54. //添加
  55. DataRowHelper.prototype.add=function(row){
  56. var isExist=false;
  57. for(var i=0;i<this.RowCollection.length;i++)
  58. {
  59. var obj=this.RowCollection[i];
  60. if(row.equals(obj))
  61. {
  62. isExist=true;
  63. }
  64. }
  65. if(!isExist)
  66. this.RowCollection.push(row);
  67. }
  68. //删除
  69. DataRowHelper.prototype.remove=function(row){
  70. for(var i=this.RowCollection.length-1;i>=0;i--)
  71. {
  72. var obj=this.RowCollection[i];
  73. if(row.equals(obj))
  74. {
  75. this.RowCollection.splice(i,1);
  76. break;
  77. }
  78. }
  79. }
  80. DataRowHelper.prototype.length=function(){
  81. return this.RowCollection.length;
  82. }
  83. //对象遍历
  84. DataRowHelper.prototype.each=function(func){
  85. var len = this.RowCollection.length;
  86. for(var i=0;i<len;i++){
  87. func(this.RowCollection[i],i);
  88. }
  89. }
  90. /**
  91. * 测试用例。
  92. */
  93. function test()
  94. {
  95. var obj1=new DataRow(1,"aa","aaa");
  96. var obj2=new DataRow(3,"aasss","aaa");
  97. var obj3=new DataRow(3,"aasss","aaa");
  98. var h=new DataRowHelper();
  99. h.add(obj1);
  100. h.add(obj2);
  101. h.add(obj3);
  102. //h.remove(obj3);
  103. h.each(detail);
  104. alert(h.length());
  105. }
  106. /**
  107. * 扩展Object类,判断两个对象是否相同,在自定义对话框中使用到
  108. */
  109. Object.prototype.equals = function(obj) {
  110. if (this == obj)
  111. return true;
  112. if (typeof (obj) == "undefined" || obj == null
  113. || typeof (obj) != "object")
  114. return false;
  115. var length = 0;
  116. var length1 = 0;
  117. for ( var ele in this) {
  118. length++;
  119. }
  120. for ( var ele in obj) {
  121. length1++;
  122. }
  123. if (length != length1)
  124. return false;
  125. if (obj.constructor == this.constructor) {
  126. for ( var ele in this) {
  127. if (typeof (this[ele]) == "object") {
  128. if (!this[ele].equals(obj[ele]))
  129. return false;
  130. } else if (typeof (this[ele]) == "function") {
  131. if (!this[ele].toString().equals(obj[ele].toString()))
  132. return false;
  133. } else if (this[ele] != obj[ele])
  134. return false;
  135. }
  136. return true;
  137. }
  138. return false;
  139. };