集合是由一组无序且唯一(即不能重复)的项组成的
1 创建集合
1.1 创建一个集合
function(){
let items = {};
}
1.2 has(value)方法
this.has = function(value){
return value in items;
};
// 或
this.has = function(value){
return items.hasOwnProperty(value);
};
1.3 add 方法
this.add = functrion(value){
if(!this.has(value)){
items[value] = value;
return true;
}
return false
}
1.4 remove方法
this.remove = function(value){
if(this.has(value)){
delete items[value];
return true;
}
return false;
}
1.5 clear 和size方法
this.clear = function(){
items = {};
}
this.size = function(){
return Object.keys(items).length;
}
1.6 获取对象 value 值
this.value = function(){
let values = [];
for(let key in items){
if(items.hasOwnProperty(key)){
values.push(items[key]);
}
}
return values;
}