準備工作:
1. MAP對象的自定義基本方法:
- keys : new Array();
- data : new Array();
- seKeyValue(key,value);
- remove(value);
- clear();
- CapArr:
/*-Map集合對象-*/
/**
* Map<key,data>
* <可自定義Key>
*/
var CapMap = function(){
var keys = new Array();
var data = new Array();
var clear = function(){
this.keys = new Array();
this.data = new Array();
};
/**
* 更新key對應的Value屬性,若不存在,添加此Key
* @param key
* @param value
*/
var setKeyValue = function(key, value) {
if (this.data[key] == null) {// 如鍵不存在則身【鍵】數組添加鍵名
this.keys.push(key);
}
this.data[key] = value;// 給鍵指派
};
var getValueByKey = function(key) {
return this.data[key];
};
var setKeys = function(keyArray){
if(typeof(keyArray)!='Array'){
alert("setKeys must insert Object as Array()");
return ;
}
this.keys=keyArray;
};
var setValues = function(dataArray){
if(typeof(dataArray)!='Array'){
alert("setValues must insert Object as Array()");
return ;
}
this.data = dataArray;
};
/*此處的繼承其實更像是将CapArr對象的引用指給了c,這樣c就也能使用CapArr對象中的方法了,
差別就是c可以擁有自己另外的屬性,即繼承的擴充*/
var remove = function(key) {
var c = {};
Object.setPrototypeOf(c, new CapArr());
c.removeElementInArray(key,this.keys);
c.removeElementInArray(data[key],this.data);
};
var isEmpty = function() {
return this.keys.length == 0;
};
var size = function() {
return this.keys.length;
};
this.data = data;
this.keys = keys;
this.init = init;
this.clear = clear;
this.setKeyValue = setKeyValue;
this.getValueByKey = getValueByKey;
this.setKeys = setKeys;
this.setValues = setValues;
this.remove = remove;
this.isEmpty = isEmpty;
this.size = size;
};
- CapMap:
/*-Map集合對象-*/
/**
* Map<key,data>
* <可自定義Key>
*/
var CapMap = function(){
var keys = new Array();
var data = new Array();
var clear = function(){
this.keys = new Array();
this.data = new Array();
};
/**
* 更新key對應的Value屬性,若不存在,添加此Key
* @param key
* @param value
*/
var setKeyValue = function(key, value) {
if (this.data[key] == null) {// 如鍵不存在則身【鍵】數組添加鍵名
this.keys.push(key);
}
this.data[key] = value;// 給鍵指派
};
var getValueByKey = function(key) {
return this.data[key];
};
var setKeys = function(keyArray){
if(typeof(keyArray)!='Array'){
alert("setKeys must insert Object as Array()");
return ;
}
this.keys=keyArray;
};
var setValues = function(dataArray){
if(typeof(dataArray)!='Array'){
alert("setValues must insert Object as Array()");
return ;
}
this.data = dataArray;
};
/*此處的繼承其實更像是将CapArr對象的引用指給了c,這樣c就也能使用CapArr對象中的方法了,
差別就是c可以擁有自己另外的屬性,即繼承的擴充*/
var remove = function(key) {
var c = {};
Object.setPrototypeOf(c, new CapArr());
c.removeElementInArray(key,this.keys);
c.removeElementInArray(data[key],this.data);
};
var isEmpty = function() {
return this.keys.length == 0;
};
var size = function() {
return this.keys.length;
};
this.data = data;
this.keys = keys;
this.init = init;
this.clear = clear;
this.setKeyValue = setKeyValue;
this.getValueByKey = getValueByKey;
this.setKeys = setKeys;
this.setValues = setValues;
this.remove = remove;
this.isEmpty = isEmpty;
this.size = size;
};