var Map = function () {
this.container = {};
};
//将key-value放入map中
Map.prototype.put = function (key, value) {
try {
if (key != null && key != "")
this.container[key] = value;
} catch (e) {
return e;
}
};
//根据key从map中取出对应的value
Map.prototype.get = function (key) {
try {
return this.container[key];
} catch (e) {
return e;
}
};
//判断map中是否包含指定的key
Map.prototype.containsKey = function (key) {
try {
for (var p in this.container) {
if (p == key)
return true;
}
return false;
} catch (e) {
return e;
}
};
//判断map中是否包含指定的value
Map.prototype.containsValue = function (value) {
try {
for (var p in this.container) {
if (this.container[p] === value)
return true;
}
return false;
} catch (e) {
return e;
}
};
//删除map中指定的key
Map.prototype.remove = function (key) {
try {
var result = this.container[key];
delete this.container[key];
return result;
} catch (e) {
return e;
}
};
//清空map
Map.prototype.clear = function () {
try {
// delete this.container;
this.container = {};
} catch (e) {
return e;
}
};
//判断map是否为空
Map.prototype.isEmpty = function () {
if (this.keys().length == 0)
return true;
else {
return false;
}
};
//获取map的大小
Map.prototype.size = function () {
return this.keys().length;
};
//返回map中的key值数组
Map.prototype.keys = function () {
var keysArr = new Array();
for (var p in this.container) {
keysArr.push(p);
}
return keysArr;
};
//返回map中的value值数组
Map.prototype.values = function () {
var valuesArr = new Array();
var keysArr = this.keys();
for (var i = 0; i < keysArr.length; i++) {
valuesArr.push(this.container[keysArr[i]]);
}
return valuesArr;
};
//改写默认toString()方法
Map.prototype.toString = function () {
var str = '{';
for (var p in this.container) {
str += p + ":" + this.container[p] + ', ';
}
if(str.length > 1) {
str = str.substring(0, str.length-2);
}
str += '}';
return str;
};
this.container = {};
};
//将key-value放入map中
Map.prototype.put = function (key, value) {
try {
if (key != null && key != "")
this.container[key] = value;
} catch (e) {
return e;
}
};
//根据key从map中取出对应的value
Map.prototype.get = function (key) {
try {
return this.container[key];
} catch (e) {
return e;
}
};
//判断map中是否包含指定的key
Map.prototype.containsKey = function (key) {
try {
for (var p in this.container) {
if (p == key)
return true;
}
return false;
} catch (e) {
return e;
}
};
//判断map中是否包含指定的value
Map.prototype.containsValue = function (value) {
try {
for (var p in this.container) {
if (this.container[p] === value)
return true;
}
return false;
} catch (e) {
return e;
}
};
//删除map中指定的key
Map.prototype.remove = function (key) {
try {
var result = this.container[key];
delete this.container[key];
return result;
} catch (e) {
return e;
}
};
//清空map
Map.prototype.clear = function () {
try {
// delete this.container;
this.container = {};
} catch (e) {
return e;
}
};
//判断map是否为空
Map.prototype.isEmpty = function () {
if (this.keys().length == 0)
return true;
else {
return false;
}
};
//获取map的大小
Map.prototype.size = function () {
return this.keys().length;
};
//返回map中的key值数组
Map.prototype.keys = function () {
var keysArr = new Array();
for (var p in this.container) {
keysArr.push(p);
}
return keysArr;
};
//返回map中的value值数组
Map.prototype.values = function () {
var valuesArr = new Array();
var keysArr = this.keys();
for (var i = 0; i < keysArr.length; i++) {
valuesArr.push(this.container[keysArr[i]]);
}
return valuesArr;
};
//改写默认toString()方法
Map.prototype.toString = function () {
var str = '{';
for (var p in this.container) {
str += p + ":" + this.container[p] + ', ';
}
if(str.length > 1) {
str = str.substring(0, str.length-2);
}
str += '}';
return str;
};