1. <SCRIPT type="text/javascript">
    2. function Map() {
    3. this.elements = new Array();
    4. //获取MAP元素个数
    5. this.size = function() {
    6. return this.elements.length;
    7. };
    8. //判断MAP是否为空
    9. this.isEmpty = function() {
    10. return (this.elements.length < 1);
    11. };
    12. //删除MAP所有元素
    13. this.clear = function() {
    14. this.elements = new Array();
    15. };
    16. //向MAP中增加元素(key, value)
    17. this.put = function(_key, _value) {
    18. this.elements.push( {
    19. key : _key,
    20. value : _value
    21. });
    22. };
    23. //删除指定KEY的元素,成功返回True,失败返回False
    24. this.remove = function(_key) {
    25. var bln = false;
    26. try {
    27. for (i = 0; i < this.elements.length; i++) {
    28. if (this.elements[i].key == _key) {
    29. this.elements.splice(i, 1);
    30. return true;
    31. }
    32. }
    33. } catch (e) {
    34. bln = false;
    35. }
    36. return bln;
    37. };
    38. //获取指定KEY的元素值VALUE,失败返回NULL
    39. this.get = function(_key) {
    40. try {
    41. for (i = 0; i < this.elements.length; i++) {
    42. if (this.elements[i].key == _key) {
    43. return this.elements[i].value;
    44. }
    45. }
    46. } catch (e) {
    47. return null;
    48. }
    49. };
    50. //获取指定索引的元素(使用element.key,element.value获取KEY和VALUE),失败返回NULL
    51. this.element = function(_index) {
    52. if (_index < 0 || _index >= this.elements.length) {
    53. return null;
    54. }
    55. return this.elements[_index];
    56. };
    57. //判断MAP中是否含有指定KEY的元素
    58. this.containsKey = function(_key) {
    59. var bln = false;
    60. try {
    61. for (i = 0; i < this.elements.length; i++) {
    62. if (this.elements[i].key == _key) {
    63. bln = true;
    64. }
    65. }
    66. } catch (e) {
    67. bln = false;
    68. }
    69. return bln;
    70. };
    71. //判断MAP中是否含有指定VALUE的元素
    72. this.containsValue = function(_value) {
    73. var bln = false;
    74. try {
    75. for (i = 0; i < this.elements.length; i++) {
    76. if (this.elements[i].value == _value) {
    77. bln = true;
    78. }
    79. }
    80. } catch (e) {
    81. bln = false;
    82. }
    83. return bln;
    84. };
    85. //获取MAP中所有VALUE的数组(ARRAY)
    86. this.values = function() {
    87. var arr = new Array();
    88. for (i = 0; i < this.elements.length; i++) {
    89. arr.push(this.elements[i].value);
    90. }
    91. return arr;
    92. };
    93. //获取MAP中所有KEY的数组(ARRAY)
    94. this.keys = function() {
    95. var arr = new Array();
    96. for (i = 0; i < this.elements.length; i++) {
    97. arr.push(this.elements[i].key);
    98. }
    99. return arr;
    100. };
    101. }
    102. $(function(){
    103. var map = new Map();
    104. map.put("key", "value");
    105. map.put("key1", "value1");
    106. map.put("key2", "value2");
    107. map.put("key3", "value3");
    108. var val = map.get("key2")
    109. alert(val);
    110. });
    111. </SCRIPT>