首先说明一下,在IE浏览器中,有一个Dictionary对象,调用方法如下:
1 var dic = new ActiveXObject("Scripting.Dictionary");
但是这个对象有局限性,只能在IE浏览器中调用,由于平时开发出来的程序要适应多浏览器,所以写了如下一个Dictionary对象:
1 /** 2 * Created by Leo on 2015/4/26. 3 */ 4 var Dictionary=function() { 5 this.elements = new Array(); 6 //Length of Dictionary 7 this.length = function () { 8 return this.elements.length; 9 }; 10 //Check whether the Dictionary is empty 11 this.isEmpty = function () { 12 return (this.length() < 1); 13 }; 14 //remove all elements from the Dictionary 15 this.removeAll = function () { 16 this.elements = new Array(); 17 }; 18 //get specify element of the dictionary 19 this.element = function (index) { 20 var rlt = null; 21 if (index >= 0 && index < this.elements.length) { 22 rlt = this.elements[index]; 23 } 24 return rlt; 25 } 26 //check whether the Dictionary contains this key 27 this.Exists = function (key) { 28 var rlt = false; 29 try { 30 for (var i = 0, iLen = this.length(); i < iLen; i++) { 31 if (this.elements[i].key == key) { 32 rlt = true; 33 break; 34 } 35 } 36 } 37 catch (ex) { 38 } 39 return rlt; 40 }; 41 //check whether the Dictionary contains this value 42 this.containsValue = function (value) { 43 var rlt = false; 44 try { 45 for (var i = 0, iLen = this.length(); i < iLen; i++) { 46 if (this.elements[i].value == value) { 47 rlt = true; 48 break; 49 } 50 } 51 } 52 catch (ex) { 53 } 54 return rlt; 55 }; 56 //remove this key from the Dictionary 57 this.remove = function (key) { 58 var rlt = false; 59 try { 60 for (var i = 0, iLen = this.length(); i < iLen; i++) { 61 if (this.elements[i].key == key) { 62 this.elements.splice(i, 1); 63 rlt = true; 64 break; 65 } 66 } 67 } 68 catch (ex) { 69 } 70 return rlt; 71 }; 72 //add this key/value to the Dictionary,if key is exists,replace the value 73 this.add = function (key, value) { 74 this.remove(key); 75 this.elements.push({ 76 key: key, 77 value: value 78 }); 79 }; 80 //add this key/value to the Dictionary,if key is exists,append value 81 this.set = function (key, value) { 82 var arr = this.getItem(key); 83 if (arr != null) { 84 if (typeof(arr) == "object") { 85 arr.unshift.apply(arr, value); 86 value = arr; 87 } 88 else { 89 var array = []; 90 array.push(arr); 91 array.unshift.apply(array, value); 92 value = array; 93 } 94 this.remove(key); 95 } 96 this.elements.push({ 97 key: key, 98 value: value 99 }); 100 } 101 //get value of the key 102 this.getItem = function (key) { 103 var rlt = null; 104 try { 105 for (var i = 0, iLen = this.length(); i < iLen; i++) { 106 if (this.elements[i].key == key) { 107 rlt = this.elements[i].value; 108 break; 109 } 110 } 111 } 112 catch (ex) { 113 } 114 return rlt; 115 }; 116 //get all keys of the dictionary 117 this.keys = function () { 118 var arr = []; 119 for (var i = 0, iLen = this.length(); i < iLen; i++) { 120 arr.push(this.elements[i].key); 121 } 122 return arr; 123 } 124 //get all values of the dictionary 125 this.values = function () { 126 var arr = []; 127 for (var i = 0, iLen = this.length(); i < iLen; i++) { 128 arr.push(this.elements[i].value); 129 } 130 return arr; 131 } 132 }