zoukankan      html  css  js  c++  java
  • 模拟实现ES6的set类

    function Set() {
        var items = {};
        // this.has = function(value){
        //     return value in items;
        // }
        this.has = function(value){
            return items.hasOwnProperty(value);
        },
        this.add = function(value){
            if (!this.has(value)) {
                items[value] = value;
                return true;
            }
            return false;
        },
        this.remove = function(value){
            if (this.has(value)) {
                delete value;
                return true;
            }
            return false;
        },
        this.clear = function(){
            items={};
        },
        this.size = function(){
            var count = 0;
            for(var prop in items){
                if (items.hasOwnProperty(prop)) {
                    ++count;
                }
            }
            return count;
        },
        this.values= function(){
            var values = [];
            for(var value in items){
                if (items.hasOwnProperty(value)) {
                    values.push(value);
                }
            }
            return values;
        },
        //并集
        this.union = function(otherSet){
            var unionSet = new Set();
            var values = this.values();
            for (var i = 0; i < values.length; i++) {
                unionSet.add(values[i]);
            }
    
            values = otherSet.values();
            for (var i = 0; i < values.length; i++) {
                unionSet.add(values[i]);
            }
    
            return unionSet;
        },
        //交集
        this.intersection = function(otherSet){
            var intersection = new Set();
            var values = this.values();
            for (var i = 0; i < values.length; i++) {
                if (otherSet.has(values[i])) {
                    intersection.add(values[i]);
                }
            }
            return intersection;
        },
        //差集
        this.difference = function(otherSet){
            var difference = new Set();
            var values = this.values();
            for (var i = 0; i < values.length; i++) {
                if (!otherSet.has(values[i])) {
                    difference.add(values[i]);
                }
            }
            return difference;
        },
        //子集
        this.subset = function(otherSet){
            var values = this.values();
            if (this.size() > otherSet.size()) {
                return false;
            }
            else{
                for (var i = 0; i < values.length; i++) {
                    if (!otherSet.has(values[i])) {
                        return false;
                    }
                }
                return true;
            }
        }
    }
    

      

  • 相关阅读:
    安装httpd服务
    tmpfs临时文件系统,是一种基于内存的文件系统
    oracle Awr报告
    maven jar 怎么看jdk编译版本
    Oracle 11g direct path read 等待事件的理解
    Spring AOP 实现原理
    JVM相关知识(1)
    spring ioc原理(看完后大家可以自己写一个spring)
    java中HashSet详解
    java中HashMap详解
  • 原文地址:https://www.cnblogs.com/ckAng/p/11118780.html
Copyright © 2011-2022 走看看