我写了一个字典对象或地图对象,无论您喜欢哪个名称,我都遇到了一个问题,在创建第二个对象后不会丢失对该对象的引用。
例如,我希望持有一个带有书籍和摘要的对象,以及一个带有汽车及其成本的对象。结果以两个映射都设置为第二个映射值结束。
function Dictionary() {
var _size = 0;
var _dict = new Object();
var _keys = new Array();
var _values = new Array();
var _firstKey = "";
var _lastKey = "";
Dictionary.prototype.put = function () {
if (_size == 0)
_firstKey = arguments[0];
_keys.push(arguments[0]);
_values.push(arguments[1]);
_dict[arguments[0]] = arguments[1];
_lastKey = arguments[0];
_size++;
};
Dictionary.prototype.firstKey = function () {
return _firstKey;
};
Dictionary.prototype.lastKey = function () {
return _lastKey;
};
Dictionary.prototype.get = function () {
return _dict[arguments[0]];
};
Dictionary.prototype.size = function () {
return _size;
};
Dictionary.prototype.entrySet = function () {
return _dict;
};
Dictionary.prototype.key = function () {
return _keys;
};
Dictionary.prototype.vaules = function () {
return _values;
};
Dictionary.prototype.clear = function () {
_size = 0;
_dict = new Object();
_keys = new Array();
_values = new Array();
_firstKey = "";
_lastKey = "";
};
Dictionary.prototype.remove = function () {
var keyIndex;
if (_size >= 1) {
for (var i = 0; i < _keys.length; i++) {
if (arguments[0] == _keys[i])
keyIndex = i;
}
if (keyIndex === undefined)
return undefined
_dict = removeItemObject(_dict, arguments[0]);
_keys = removeItemArray(_keys, keyIndex);
_values = removeItemArray(_values, keyIndex);
if (_keys.length > 0 && _keys.length == keyIndex) {
_lastKey = _keys[keyIndex - 1];
}
if (_keys.length == 0)
_lastKey = undefined;
if (0 == keyIndex) {
if (_keys.length > 1)
_firstKey = _keys[1];
else if (_keys.length > 0)
_firstKey = _keys[0];
else if (_keys.length == 0)
_firstKey = undefined;
}
_size--;
}
};
Dictionary.prototype.serialize = function () {
var serializedFJSON = "{";
serializedFJSON += "\"size\"" + ":" + _size + ",";
serializedFJSON += "\"dict\"" + ":" + JSON.stringify(_dict) + ",";
serializedFJSON += "\"keys\"" + ":" + JSON.stringify(_keys) + ",";
serializedFJSON += "\"values\"" + ":" + JSON.stringify(_values) + ",";
serializedFJSON += "\"firstKey\"" + ":" + "\"" + _firstKey + "\"" + ",";
serializedFJSON += "\"lastKey\"" + ":" + "\"" + _lastKey + "\"" + "";
serializedFJSON += "}";
return serializedFJSON;
};
Dictionary.prototype.deserialize = function () {
var DictionaryClone = JSON.parse(arguments[0]);
_size = DictionaryClone.size;
_dict = DictionaryClone.dict;
_keys = DictionaryClone.keys;
_values = DictionaryClone.values;
_firstKey = DictionaryClone.firstKey;
_lastKey = DictionaryClone.lastKey;
};
function removeItemArray(arrayName, key) {
var x;
var tmpArray = new Array();
for (x in arrayName) {
if (x != key) { tmpArray[x] = arrayName[x]; }
}
return tmpArray;
};
function removeItemObject(arrayName, key) {
var x;
var tmpArray = new Object();
for (x in arrayName) {
if (x != key) { tmpArray[x] = arrayName[x]; }
}
return tmpArray;
};
}
var m = new Dictionary();
m.put("Lord Of The Rings", "Not One Book But, Three");
m.put("Curious George", "I Don't Know Something About a Guy In A Rain Coat");
var k = new Dictionary();
k.put("Scion FRS", "24955");
k.put("Toyota Camry", "22055");
k.remove("Toyota Camry");
for (items in m.entrySet()) {
alert(items + " " + m.entrySet()[items]);
}