JavaScript中用數組實現鍵值對

<script type="text/javascript" language="javascript">
//自定義字典對象
function Dictionary(){
 this.data = new Array();
 
 this.put = function(key,value){
  this.data[key] = value;
 };

 this.get = function(key){
  return this.data[key];
 };

 this.remove = function(key){
  this.data[key] = null;
 };
 
 this.isEmpty = function(){
  return this.data.length == 0;
 };

 this.size = function(){
  return this.data.length;
 };
}

//使用 例子
var d = new Dictionary();
d.put("CN", "China");
d.put("US", "America");
document.write(d.get("CN"));
</script>
相關文章
相關標籤/搜索