function Set() {
let items = {};
//has(value)方法
//1
this.has = function(value) {
return value in items;
};
//2
this.has = function(value) {
return items.hasOwnProperty(value);
};
//add方法
this.add = function(value) {
if(!this.has(value)){
items[value] = value;
return true;
}
return true;
};
//remove方法
this.remove = function(value) {
if(this.has(value)) {
delete items[value];
return true;
}
return false;
};
//clear方法
this.clear = function() {
items = {};
};
//size方法
//1
this.size = function() {
return Object.keys(items).length;
};
//2
this.size = function() {
let count = 0;
for(let key in items) {
if(items.hasOwnProperty(key))
++count;
}
return count;
};
//values方法
//1
this.values = function() {
let values = [];
for (let i=0, keys=Object.keys(items); i<keys.length; i++) {
values.push(items[key]);
}
return values;
};
//2
this.values = function() {
let values = [];
for(let key in items) {
if(items.hasOwnProperty(key)) {
values.push(items[key]);
}
}
return values;
}
}
集合(set类)
猜你喜欢
转载自blog.csdn.net/weixin_39466493/article/details/80944465
今日推荐
周排行