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;
            }
        }
    }
    

      

  • 相关阅读:
    Redis学习笔记
    Sevrlet 工作原理解析-转
    Spring MVC基础学习
    JVMGC机制
    MyBatis基础学习笔记--摘录
    MyBatis基础学习笔记--自总结
    Spring基础复习
    java.lang.Class类
    浅谈linux静态库、动态库。
    关于字符编码的一点学习。
  • 原文地址:https://www.cnblogs.com/ckAng/p/11118780.html
Copyright © 2011-2022 走看看