1 // js通用方法 2 3 // map对象定义 4 function Map() { 5 var struct = function(key, value) { 6 this.key = key; 7 this.value = value; 8 }; 9 10 var put = function(key, value) { 11 for (var i = 0; i < this.arr.length; i++) { 12 if (this.arr[i].key === key) { 13 this.arr[i].value = value; 14 return; 15 } 16 } 17 this.arr[this.arr.length] = new struct(key, value); 18 }; 19 20 var get = function(key) { 21 for (var i = 0; i < this.arr.length; i++) { 22 if (this.arr[i].key === key) { 23 return this.arr[i].value; 24 } 25 } 26 return null; 27 }; 28 29 var remove = function(key) { 30 var v; 31 for (var i = 0; i < this.arr.length; i++) { 32 v = this.arr.pop(); 33 if (v.key === key) { 34 continue; 35 } 36 this.arr.unshift(v); 37 } 38 }; 39 40 var size = function() { 41 return this.arr.length; 42 }; 43 44 var isEmpty = function() { 45 return this.arr.length <= 0; 46 }; 47 this.arr = new Array(); 48 this.get = get; 49 this.put = put; 50 this.remove = remove; 51 this.size = size; 52 this.isEmpty = isEmpty; 53 }