JS中的Map

原文:點擊打開鏈接

var map = new Map();

 map.put("a", "aaa");
 map.put("b","bbb");
 map.put("cc","cccc");
 map.put("c","ccc");
 map.remove("cc");
 var array = map.keySet();
 for(var i in array) {
 document.write("key:(" + array[i] +") <br>value: ("+map.get(array[i])+") <br>");
}


function Map(){
this.container = new Object();
}


Map.prototype.put = function(key, value){
this.container[key] = value;
}



Map.prototype.get = function(key){
return this.container[key];
}


Map.prototype.keySet = function() {
var keyset = new Array();
var count = 0;
for (var key in this.container) {
// 跳過object的extend函數
if (key == 'extend') {
continue;
}
keyset[count] = key;
count++;
}
return keyset;
}


Map.prototype.size = function() {
var count = 0;
for (var key in this.container) {
// 跳過object的extend函數
if (key == 'extend'){
continue;
}
count++;
}
return count;
}


Map.prototype.remove = function(key) {
delete this.container[key];
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章