JavaScript数据结构——模仿ES6中定义的类似的Set类

  1 function Set(){
  2     var items={};
  3 
  4     this.has=function(value){
  5         return items.hasOwnProperty(value);
  6     };
  7 
  8     this.add=function(value){
  9         if(!this.has(value)){
 10             items[value]=value;
 11             return true;
 12         }
 13         return false;
 14     };
 15 
 16     this.remove=function(value){
 17         if(this.has(value)){
 18             delete items[value];
 19             return true;
 20         }
 21         return false;
 22     };
 23 
 24     this.clear=function(){
 25         items={};
 26     };
 27 
 28     //方法一:ES5以上
 29 /*    this.size=function(){
 30         return Object.keys(items).length;
 31     }*/
 32 
 33     //方法二:兼容新较强
 34     //hasOwnProperty()用于排除items原型自身的属性
 35     this.size=function(){
 36         var count=0;
 37         for(var prop in items){
 38             if(items.hasOwnProperty(prop)){
 39                 count++;
 40             }
 41         }
 42         return count;
 43     };
 44 
 45     //方法一:ES5以上
 46     /*this.values=function(){
 47         return Object.keys(items);
 48     };*/
 49 
 50     //兼容性比较强
 51     this.values=function(){
 52         var keys=[];
 53         for(var key in items){
 54             if(items.hasOwnProperty(key)){
 55                 keys.push(key);
 56             }
 57         }
 58         return keys;
 59     };
 60 
 61 
 62     //集合的并操作
 63     this.union=function(otherSet){
 64         var unionSet = new Set();
 65         var values=this.values();
 66         for(var i=0;i<values.length;i++){
 67             unionSet.add(values[i]);
 68         }
 69 
 70         values=otherSet.values();
 71         for(i;i<values.length;i++){
 72             unionSet.add(values[i]);
 73         }
 74         return unionSet;
 75     };
 76 
 77     //集合的交集
 78     this.intersection=function(otherSet){
 79         var intersectionSet = new Set();
 80         var values=this.values();
 81         for(var i=0;i<values.length;i++){
 82             if(otherSet.has(values[i])){
 83                 intersectionSet.add(values[i]);
 84             }
 85         }
 86         return intersectionSet;
 87     };
 88 
 89     //差集操作
 90     this.difference=function(otherSet){
 91         var differenceSet = new Set();
 92         var values=this.values();
 93         for(var i=0;i<values.length;i++){
 94             if(!otherSet.has(values[i])){
 95                 differenceSet.add(values[i]);
 96             }
 97         }
 98         return differenceSet;
 99     };
100 
101     //判断是否是otherSet的子集
102     this.subSet=function(otherSet){
103         if(this.size()>otherSet.size()){
104             return false;
105         }else{
106             var values=this.values();
107             for(var i=0;i<values.length;i++){
108                 if(!otherSet.has(values[i])){
109                     return false;
110                 }
111             }
112             return true;
113         }
114 
115     }

这个实现中,比较需要注意的是,set类即是数学概念上的集合,集合中的元素不得重复,具有唯一性。所以用对象初始化items,可以利用键值对的唯一性特点实现集合的性质。相关的集合性质可以查看数学集合概念知识。上述例子可以自行编写一些简单的html例子实现。

posted @ 2017-04-21 07:35  夏时未祭  阅读(256)  评论(0编辑  收藏  举报