// 1.const ws =newWeakSet();// ws.add(1); // Invalid value used in weak set// 因为垃圾回收j机制何时运行是不可预测的,因此ES6规定WeakSet不可遍历// 1.[1,2]被解析为对象const a =[[1,2],[3,4]];const ws2 =newWeakSet(a);for(let i of a){
console.log(i)}const b =[3,4];// const ws3 = new WeakSet(b) //Invalid value used in weak set// 方法/*
add(value),delete(value),has(value)
*/// egconst ws4 =newWeakSet();const obj ={};const foo ={};
ws4.add(obj);
console.log(ws4.has(obj),ws4.has(foo),ws4.size);// eg2const foos =newWeakSet();classFoo{constructor(){
foos.add(this);};method(){if(!foos.has(this)){thrownewTypeError('Foo.protoType.method只能在Foo的实例上调用!');}}}