日期:2014-05-16 浏览次数:20386 次
1.<!-- 2. var Map=function(){ 3. this.mapArr={}; 4. this.arrlength=0; 5. 6. //假如有重复key,则不存入 7. this.put=function(key,value){ 8. if(!this.containsKey(key)){ 9. this.mapArr[key]=value; 10. this.arrlength=this.arrlength+1; 11. } 12. } 13. this.get=function(key){ 14. return this.mapArr[key]; 15. } 16. 17. //传入的参数必须为Map结构 18. this.putAll=function(map){ 19. if(Map.isMap(map)){ 20. var innermap=this; 21. map.each(function(key,value){ 22. innermap.put(key,value); 23. }) 24. }else{ 25. alert("传入的非Map结构"); 26. } 27. } 28. this.remove=function(key){ 29. delete this.mapArr[key]; 30. this.arrlength=this.arrlength-1; 31. } 32. this.size=function(){ 33. return this.arrlength; 34. } 35. 36. //判断是否包含key 37. this.containsKey=function(key){ 38. return (key in this.mapArr); 39. } 40. //判断是否包含value 41. this.containsValue=function(value){ 42. for(var p in this.mapArr){ 43. if(this.mapArr[p]==value){ 44. return true; 45. } 46. } 47. return false; 48. } 49. //得到所有key 返回数组 50. this.keys=function(){ 51. var keysArr=[]; 52. for(var p in this.mapArr){ 53. keysArr[keysArr.length]=p; 54. } 55. return keysArr; 56. } 57. //得到所有value 返回数组 58. this.values=function(){ 59. var valuesArr=[]; 60. for(var p in this.mapArr){ 61. valuesArr[valuesArr.length]=this.mapArr[p]; 62. } 63. return valuesArr; 64. } 65. 66. this.isEmpty=function(){ 67. if(this.size()==0){ 68. return false; 69. } 70. return true; 71. } 72. this.clear=function(){ 73. this.mapArr={}; 74. this.arrlength=0; 75. } 76. //循环 77. this.each=function(callback){ 78. for(var p in this.mapArr){ 79. callback(p,this.mapArr[p]); 80. } 81. 82. } 83. 84. 85. } 86. //判断是否是map对象 87. Map.isMap=function(map){ 88. return (map instanceof Map); 89. } 90. 91. 92. var map=new Map(); 93. map.put("afei",25); 94. map.put("yaoming",31); 95. map.put("pp",2); 96. map.put("bill","55"); 97. map.remove("afei"); 98. 99. var map2=new Map(); 100. map2.put("003",333); 101. map2.put("004",444); 102. map.putAll(map2); 103. 104. 105. map.each(function(key,value){ 106. alert(key+" : "+value); 107. }) 108. 109. 110. 111. //--> 112.