function unique(arr){
if(!isArrayLink(arr)){
return arr
}
let result = []
let objarr = []
let obj = Object.create(null)
arr.forEach(item => {
if(isStatic(item)){
let key = item + '_' + getRawType(item);
if(!obj[key]){
obj[key] = true
result.push(item)
}
}else{
if(!objarr.includes(item)){
objarr.push(item)
result.push(item)
}
}
})
return resulte
}
Set简单实现
window.Set = window.Set || (function () {
function Set(arr) {
this.items = arr ? unique(arr) : [];
this.size = this.items.length;
}
Set.prototype = {
add: function (value) {
if (!this.has(value)) {
this.items.push(value);
this.size++;
}
return this;
},
clear: function () {
this.items = []
this.size = 0
},
delete: function (value) {
return this.items.some((v, i) => {
if(v === value){
this.items.splice(i,1)
return true
}
return false
})
},
has: function (value) {
return this.items.some(v => v === value)
},
values: function () {
return this.items
},
}
return Set;
}());