Set(放数组,不能重复)
会自动去重,有4个元素,但是打印出来只有三个
let setArr=new Set(['zhang','huan','eternity','zhang']);
console.log(setArr);//{"zhang", "huan", "eternity"}
//添加元素
setArr.add('zhanghuan');
console.log(setArr);//{"zhang", "huan", "eternity", "zhanghuan"}
//查找元素
setArr.has('zhang');
console.log(setArr.has('zhang'));//true
//删除元素
setArr.delete('huan');//true
console.log(setArr);//{"zhang", "eternity", "zhanghuan"}
//循环打印
for(let item of setArr){
console.log(item);
}
setArr.forEach(item=>console.log(item));
//长度(属性,不是方法)
setArr.size;
WeakSet(可以放对象,也是不能重复)
let weakObj=new WeakSet();//不能直接放对象,需要添加
let obj={'a':'eternity','b':'zhang'};
weakObj.add(obj);
console.log(weakObj);
let obj1={'a':'eternity','b':'zhang'};
weakObj.add(obj1);
console.log(weakObj);//此时有两个值,因为obj和obj1的内存地址不一样