ES 6個人メモ——WeakSetデータ構造
6259 ワード
Set ,
1.WeakSet ,
2.WeakSet ,
WeakSet
: ,
,
WeakSet
// 1.
const ws = new WeakSet();
// ws.add(1); // Invalid value used in weak set
// j , ES6 WeakSet
// 1.[1,2]
const a = [[1,2],[3,4]];
const ws2 = new WeakSet(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)
*/
// eg
const ws4 = new WeakSet();
const obj = {
};
const foo = {
};
ws4.add(obj);
console.log(ws4.has(obj),ws4.has(foo),ws4.size);
// eg2
const foos = new WeakSet();
class Foo {
constructor(){
foos.add(this);
};
method(){
if(!foos.has(this)){
throw new TypeError('Foo.protoType.method Foo !');
}
}
}