集合的js实现

function set(){
    var items={}
    this.has=function(value){
        return items.hasOwnProperty(value)
    }
    this.add=function(value){
        if(!this.has(value)){
            items[value]=value
        
            return true
        }else{
            return false
        }
    }
    this.remove=function(value){
       if(this.has(value)){
        delete items[value]
        return true
       }else{
        return false
       }

    }
    this.clear=function(){
        items={}
    }
    this.size=function(){
          var count=0
          for(var prop in items){ 
            if(this.has(prop)){   //因为对象圆形还包含别的自带继承属性 用has保险 
                ++count
            }
          }
         return count
    }
    this.values=function(){
        var keys=[]
        for(var prop in items){
            if(this.has(prop)){
                keys.push(prop)
            }
        }
        return keys
    }
    this.union=function(otherSet){  //合集
        var unionSet=new set()
        var values=this.values();
        for(var i=0;i<values.length;i++){
            unionSet.add(values[i])
        }

        var values=otherSet.values();
        for(var i=0;i<values.length;i++){
            unionSet.add(values[i])
        }
        return unionSet
     }
     this.intersection=function(otherSet){  //交集
           var intersectionSet=new set()
           var values=this.values()
           for(var i=0;i<values.length;i++){
                if(otherSet.has(values[i])){
                    intersectionSet.add(values[i])
                }

           }
           return intersectionSet
     }
    this.unintersection=function(otherSet){  //差集
           var intersectionSet=new set()
           var values=this.values()
           for(var i=0;i<values.length;i++){
                if(!otherSet.has(values[i])){
                    intersectionSet.add(values[i])
                }

           }
           return intersectionSet
     }
     this.son=function(otherSet){    //子集
        if(this.size()>otherSet.size()){
            return false
        }else{
             var values=this.values()
             for(var i=0;i<values.length;i++){
                if(!otherSet.has(values[i])){
                    return false
                }
             }
             return true

        }

     }
}

 var see=new set()
 see.add(1)
 see.add(2)


var see2=new set()
see2.add(1)
see2.add(5)
see2.add(7)


var see3=new set()
see3.add(5)

var ab=see.union(see2)
console.log(ab.values())
var cb=see.intersection(see2)
console.log(cb.values())

var cj=see.unintersection(see2)
console.log(cj.values())


console.log(see3.son(see))
console.log(see3.son(see2))
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 单例模式 适用场景:可能会在场景中使用到对象,但只有一个实例,加载时并不主动创建,需要时才创建 最常见的单例模式,...
    Obeing阅读 2,110评论 1 10
  • Address:https://www.zybuluo.com/XiangZhou/note/208532 Exp...
    天蠍蒗漫阅读 11,408评论 2 55
  • 工厂模式类似于现实生活中的工厂可以产生大量相似的商品,去做同样的事情,实现同样的效果;这时候需要使用工厂模式。简单...
    舟渔行舟阅读 7,854评论 2 17
  • 夏末初秋的夜晚,午后的暑热渐消,秋夜的凉风轻轻吹进窗口,临睡前不经意的一次转台,与这部《海街日记》就这样不期而遇。...
    相楚阅读 1,863评论 2 5
  • 今天在家几乎写了一天的作业,也不知道是不是所有小朋友都这样,反正涵涵的拼音学的不是很好。说出一个字,拼音要么拼不出...
    仲思涵妈妈阅读 180评论 0 0