JavaScript实现简单的Map

function Map(){
	if(this === window){
		return new Map();
	}else{
		//alert(this instanceof Map); //true
		return this;
	}
}

Map.prototype = {
	construct:Map,
	map:{},
	size:function(){
		var len = 0;
		for(k in this.map){
			len++;
		}
		return len;
	},
	put:function(key,val){
		this.map[key] = val;
	},
	get:function(key){
		return this.map[key];
	},
	remove:function(key){
		delete this.map[key];
	},
	keySet:function(){
		var keyset = [];
		for(key in this.map){
			keyset.push(key);
		}
		return keyset;
	},
	valueSet:function(){
		var valueset = [];
		for(key in this.map){
			valueset.push(this.map[key]);
		}
		return valueset;
	}
}

var map = new Map();
map.put('a','1234');
map.put('b','23232323');
map.put('c','aaaa');
map.put('d','ffffff');
map.remove('c');
alert(map.size())
alert(map.keySet())
alert(map.valueSet())


你可能感兴趣的:(JavaScript实现简单的Map)