/**
* Map 的方法
*/
Map = function(){
var mapAddM = {
/**
* entry函数
* @param {Object} key
* @param {Object} val
*/
entry: function(key, val){
this.key = key;
this.value = val;
},
//put方法
put: function(key, val){
this.store[this.store.length] = new this.entry(key, val);
},
//get方法
get: function(key){
for (var i = 0; i < this.store.length; i++) {
if (this.store[i].key === key)
return this.store[i].value;
}
},
//remove方法
remove: function(key){
for (var i = 0; i < this.store.length; i++) {
this.store[i].key === key && this.store.splice(i, 1);
}
},
//keyset
keySet: function(){
var keyset = new Array;
for (var i = 0; i < this.store.length; i++)
keyset.push(this.store[i].key);
return keyset;
},
//valset
valSet: function(){
var valSet = new Array;
for (var i = 0; i < this.store.length; i++)
valSet.push(this.store[i].value);
return valSet;
},
//clear
clear: function(){
this.store.length = 0;
},
//size
size: function(){
return this.store.length;
},
/**
* 迭代子
*/
iterator : function(){
//TODO 待实现
var obj = this.keySet();//所有的key集合
var idx = 0;
var me = {
/**
* 当前key
*/
current : function(){
return obj[idx-1];
},
/**
* 第一个key
*/
first : function(){
return obj[0];
},
/**
* 最后一个key
*/
last : function(){
return obj[obj.length-1];
},
/**
* 是否还有下一个元素
*/
hasNext : function(){
idx++;
if(idx > obj.length||null==obj[obj.length-1])return false;
return true;
}
};
return me;
}
};
for (var method in mapAddM) {
this.store = new Array;
Map.prototype[method] = mapAddM[method];
}
}