js数据结构--集合

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>



<script type="text/javascript">
    1.集合(Set):集合里的元素是唯一的
    2.集合可用的方法
        add(value):向集合中添加一项
        remove(value):从集合中移除一项
        has(value):如果值在集合中,返回true,否则返回false
        clear():移除集合中所有的项
        size():返回集合中包含元素的数量
        values():返回一个包含集合中所有值的数组
    3.has(value)方法:
        this.has=function(value){
            return value in items;
        }
    4.add(value)方法:
        this.add=function(value){
            if(!has(value)){
                items[value]=value;
                return true;
            }
            return false;
        }
    5.remove(value)方法
        this.remove=function(value){
            if(has(value)){
                delete items[remove];
                return true;
            }
            return false;
        }
    6.clear()方法:
        this.clear=function(){
            items={}
        }
    7.size()
        this.size=function(){
            return Object.keys(items).length;
        }
    8.values()
        this.values=function(){
            return Object.keys(items);
        }
</script>
</body>
</html>

 

posted @ 2017-11-07 18:42  逗比煎饼侠  阅读(165)  评论(0编辑  收藏  举报