您的位置:首页 > Web前端 > JavaScript

Map.js

2013-12-26 17:49 113 查看
(function() {
var Map, map;
var __hasProp = Object.prototype.hasOwnProperty;
Map = function() {
this.entry = {};
this.count = 0;
return this;
};
Map.prototype.size = function() {
return this.count;
};
Map.prototype.isEmpty = function() {
return this.count === 0;
};
Map.prototype.containsKey = function(key) {
if (this.isEmpty()) {
return false;
}
return this.entry.hasOwnProperty(key);
};
Map.prototype.containsValue = function(val) {
var _a, _val, key;
if (this.isEmpty()) {
return false;
}
_a = this.entry;
for (key in _a) {
if (!__hasProp.call(_a, key)) continue;
_val = _a[key];
if (_val === val) {
return true;
}
}
return false;
};
Map.prototype.get = function(key) {
if (this.isEmpty()) {
return null;
}
if (this.containsKey(key)) {
return this.entry[key];
}
return null;
};
Map.prototype.put = function(key, val) {
if (!this.entry.hasOwnProperty(key)) {
this.count += 1;
};
this.entry[key] = val;
return this;
};
Map.prototype.remove = function(key) {
if (this.isEmpty()) {
return false;
}
if (this.containsKey(key)) {
delete this.entry[key];
this.count -= 1;
return true;
}
return false;
};
Map.prototype.putAll = function(map) {
var _a, entry, key, val;
if (!map instanceof Map) {
return false;
}
entry = map.entry;
_a = entry;
for (key in _a) {
if (!__hasProp.call(_a, key)) continue;
val = _a[key];
this.put(key, val);
}
return true;
};
Map.prototype.clear = function() {
this.entry = {};
this.count = 0;
return this;
};
Map.prototype.values = function() {
var _a, key, val, vals;
vals = [];
_a = this.entry;
for (key in _a) {
if (!__hasProp.call(_a, key)) continue;
val = _a[key];
vals.push(val);
}
return vals;
};
Map.prototype.keySet = function() {
var _a, key, keys, val;
keys = [];
_a = this.entry;
for (key in _a) {
if (!__hasProp.call(_a, key)) continue;
val = _a[key];
keys.push(key);
}
return keys;
};
Map.prototype.entrySet = function() {
return this.entry;
};
Map.prototype.toString = function() {
if (typeof JSON === "undefined") {
throw new Error("JSON object is not supported. Please check your browser version (IE8+, Firefox11+, Chrome19+, Safari5.1+).");
}
return JSON.stringify(this.entry);
};
Map.prototype.valueOf = function() {
return this.toString();
};
})();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: