JavaScript数据结构-10.字典

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="UTF-8">
 5         <title></title>
 6     </head>
 7     <body>
 8         <script>
 9             function Dictionary(){
10                 this.arr = new Array();
11                 this.add = add;    
12                 this.show = show;
13                 this.remove = remove;
14                 this.find = find;
15                 this.length = length;
16             }
17             function add(key,value){
18                 this.arr[key] = value;
19             }
20             function find(key){
21                 return this.arr[key];
22             }
23             function remove(key){
24                 delete this.arr[key];
25             }
26             function show(){
27                 for (var key in this.arr){
28                     console.log(key +" ->"+this.arr[key]);
29                 }
30             }
31             
32             function length(){
33                 var len=0;
34                 for(var key in this.arr){
35                     len++;
36                 }
37                 return len;
38             }
39             
40             function clear(){
41                 for(var key in this.arr){
42                     delete this.arr[key];
43                 }
44             }
45             
46             var obj = new Dictionary();
47             obj.add("name","zhangsan");
48             obj.add("age","20");
49             obj.add("phone","130123")
50             
51             obj.show();
52             console.log(obj.length());
53             obj.remove("age");
54             obj.show();
55             console.log(obj.find("phone"));
56         </script>
57     </body>
58 </html>

 

posted @ 2017-07-16 21:25  橙云生  阅读(175)  评论(0编辑  收藏  举报