commonMap.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. *
  3. * JavaScript - CommonUtils
  4. */
  5. // js模拟java中map,提供Map的add get remove cut方法----start
  6. (function(win) {
  7. var FastMap = function(){
  8. this.version = '1.0'; // 版本标识
  9. this.buf = new Object(); // 缓存Key和Value的对象
  10. };
  11. FastMap.prototype = {
  12. put:function(sKey, oValue){
  13. this.buf[sKey] = oValue;
  14. },
  15. get:function(sKey){
  16. return this.buf[sKey];
  17. },
  18. remove:function(sKey){
  19. delete(this.buf[sKey]);
  20. },
  21. cut:function(sKey){
  22. var buf = this.buf;
  23. var result = buf[sKey];
  24. delete buf[sKey];
  25. return result;
  26. },
  27. getBuf:function(){
  28. return this.buf;
  29. },
  30. size:function(){
  31. var buf = this.buf;
  32. var i = 0;
  33. for(var ele in buf){
  34. i++;
  35. }
  36. return i;
  37. },
  38. toJson:function(){
  39. var b = this.buf;
  40. var buf = [];
  41. for(var ele in b){
  42. buf.push('Key:');
  43. buf.push(ele);
  44. buf.push(' Value:');
  45. buf.push(b[ele]);
  46. buf.push('\n');
  47. }
  48. return buf.join('');
  49. },
  50. toString:function(){
  51. var b = this.buf;
  52. var buf = [];
  53. buf.push("{");
  54. var index = 0
  55. for(var ele in b){
  56. if(index == 0){
  57. buf.push("\""+ele+"\":");
  58. }else{
  59. buf.push(",\""+ele+"\":");
  60. }
  61. if(b[ele].constructor==String){
  62. buf.push("\""+b[ele]+"\"");
  63. }else{
  64. buf.push(b[ele]);
  65. }
  66. index++;
  67. }
  68. buf.push("}");
  69. return buf.join('');
  70. },
  71. toIterator:function(){
  72. var key = [];
  73. var b = this.buf;
  74. for(var ele in b){
  75. key.push(ele);
  76. }
  77. return key;
  78. },
  79. clear:function(){
  80. var b = this.buf;
  81. for(var ele in b){
  82. delete(this.buf[ele]);
  83. }
  84. },
  85. toStrings:function(){
  86. var b = this.buf;
  87. var buf = [];
  88. for(var ele in b){
  89. buf.push(b[ele]);
  90. }
  91. return buf.join('');
  92. },
  93. toStringKeys:function(){
  94. var key = [];
  95. var b = this.buf;
  96. for(var ele in b){
  97. key.push(ele);
  98. }
  99. return key.toString();
  100. }
  101. };
  102. win.FastMap = FastMap;
  103. })(window);
  104. //js模拟java中map,提供Map的add get remove cut方法----end