首页 > 技术文章 > JS 自定义字典对象

wzq806341010 2014-04-29 23:43 原文

  <script type="text/javascript" language="javascript">
//自定义字典对象
function Dictionary(){
 this.data ={};
 
 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",{id:1,name:'df'});
d.put("US", "America");
document.write(d.get("CN").name);
document.write(d.get("US"));


var dic={};
dic.a=1;
dic["b"]={id:1,name:"wqeqweqwewqe"}
function get(key){
    return dic[key];
}
document.write(get("b").name);
</script>

 

推荐阅读