// 集合(不允许重复)es6提出了Set数据结构,也就是我们的集合
//这里我们通过对象来模拟集合,适用对象而不使用数组的原因是因为对象不允许有重复的属性
//是将集合中的值作为对象的键来定义
function Set(){
this.items = {};
//判断集合中有没有某一个值
this.has = function(val){
return this.items.hasOwnProperty(val);
}
//给集合中添加元素
this.add = function(val){
if(!this.has(val)){
this.items[val] = val;
return true;
}else{
return false;
}
}
//删除集合中某一个元素
this.remove = function(val){
if(this.has(val)){
delete this.items[val];
return true;
}else{
return false;
}
}
//清空集合
this.clear = function(){
this.items = {};
}
//统计集合中的元素个数
//方法一
// this.size = function () {
js实现集合
最新推荐文章于 2022-10-06 19:56:42 发布