首页 > 代码库 > JS 自定义字典对象

JS 自定义字典对象

  <script type="text/javascript" language="javascript">
//自定义字典对象
function Dictionary(){
 this.data =http://www.mamicode.com/{};
 
 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>