在一些低版本的浏览器中是不支持Map对象的,所以就梦想自己写一个出来,有些时候Map要比数组好用。
/**
* Created by 李志超 on 2017/1/7.
*
*/
var Map = function () {
var keySet = [];
this.size = function () {
return keySet.length;
};
this.keys = function () {
return keySet;
};
this.get = function (key) {
return this[key];
};
this.put = function (key, val) {
this[key] = val;
keySet.push(key);
return this;
};
this.remove = function (key) {
var index = keySet.indexOf(key);
if (index >= 0) {
keySet.splice(index, 1);
delete this[key];
}
return this;
};
this.clear = function () {
keySet = [];
return this;
}
};
var map = new Map();