<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript">
/**
* Map
*
* var map = new Map();
*
* map.put(_key,_value);
*
* map.get(_key);
*
* map.remove(_key);
*
* map.clean();
*
* map.size();
*
* map.contains(_key);
*
*/
function Map(){
this._length = 0;
this._data = {};
/*this.get = function(){
}*/
}
/**
* 根据key获取一个值
* @param _key
* @returns {*}
*/
Map.prototype.get = function(_key){
//先判断是否存在
if(this.contains(_key)){
return this._data[_key+""];
}
};
/**
* 放一个键值对到map中
* @param _key
* @param _value
*/
Map.prototype.put = function(_key,_value){
//先判断是否存在
if(this.contains(_key)){
//表示更新
this._data[_key+""] = _value;
}else{
//表示新增
this._data[_key+""] = _value;
//判断是否添加上
if(this.contains(_key)){
//维护length属性
this._length++;
}
}
};
/**
* 根据传入的key去上传一个元素
* @param _key
*/
Map.prototype.remove = function(_key){
//先判断是否存在
if(this.contains(_key)){
//删除元素
delete this._data[_key+""];
//判断是否删除成功
if(!this.contains(_key)){
//维护length
this._length--;
}
}
};
/**
* 到map中元素的格个数
* @returns {number}
*/
Map.prototype.size = function(){
return this._length;
};
/**
* 清空map
*/
Map.prototype.clean = function(){
this._length = 0;
this._data = {};
};
/**
* 根据传入的key 去判断在map中是否存在
* @param _key
* @returns {boolean}
*/
Map.prototype.contains = function(_key){
return _key+"" in this._data;
};
Map.prototype.each = function(callback){
for(var _key in this._data){
callback(_key,this._data[_key]);
}
}
//each使用实例:
var map = new Map();
map.put("1","100")
map.put("2","100")
map.each(function(_key,_value){
console.log(_key+":"+_value);
})
</script>
</head>
<body>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript">
/**
* Map
*
* var map = new Map();
*
* map.put(_key,_value);
*
* map.get(_key);
*
* map.remove(_key);
*
* map.clean();
*
* map.size();
*
* map.contains(_key);
*
*/
function Map(){
this._length = 0;
this._data = {};
/*this.get = function(){
}*/
}
/**
* 根据key获取一个值
* @param _key
* @returns {*}
*/
Map.prototype.get = function(_key){
//先判断是否存在
if(this.contains(_key)){
return this._data[_key+""];
}
};
/**
* 放一个键值对到map中
* @param _key
* @param _value
*/
Map.prototype.put = function(_key,_value){
//先判断是否存在
if(this.contains(_key)){
//表示更新
this._data[_key+""] = _value;
}else{
//表示新增
this._data[_key+""] = _value;
//判断是否添加上
if(this.contains(_key)){
//维护length属性
this._length++;
}
}
};
/**
* 根据传入的key去上传一个元素
* @param _key
*/
Map.prototype.remove = function(_key){
//先判断是否存在
if(this.contains(_key)){
//删除元素
delete this._data[_key+""];
//判断是否删除成功
if(!this.contains(_key)){
//维护length
this._length--;
}
}
};
/**
* 到map中元素的格个数
* @returns {number}
*/
Map.prototype.size = function(){
return this._length;
};
/**
* 清空map
*/
Map.prototype.clean = function(){
this._length = 0;
this._data = {};
};
/**
* 根据传入的key 去判断在map中是否存在
* @param _key
* @returns {boolean}
*/
Map.prototype.contains = function(_key){
return _key+"" in this._data;
};
Map.prototype.each = function(callback){
for(var _key in this._data){
callback(_key,this._data[_key]);
}
}
//each使用实例:
var map = new Map();
map.put("1","100")
map.put("2","100")
map.each(function(_key,_value){
console.log(_key+":"+_value);
})
</script>
</head>
<body>
</body>
</html>