웹개발/ES6, ES2020
[ES6] weakMap과 weakSet
kmhan
2020. 8. 4. 00:35
728x90
반응형
weakMap과 weakSet의 정의
- 해당 객체는 object만을 키로 허용하고 값은 임의의 값을 허용하는 키/값 형태 요소의 집합
- 객체 외에 특정 type의 값을 저장할 수 없다.
- 윈도우 파일의 바로가기 같은 역할이다. (용량을 차지하지 않기 때문)
그러므로 가비지 콜렉터의 실행 대상이 아니다.
1. weakMap
- 객체 중복가능
- 주어진 객체가 map안에 존재하는지 확인하는 용도
- 관련 메서드
1) WeakMap.prototype.delete(key) : delete되면 has할때 false 반환
2) WeakMap.prototype.get(key) : 관련값이 없으면 undefined 반환
3) WeakMap.prototype.has(key) : boolean 반환
4) WeakMap.prototype.set(key) : 객체 내 key에 대해 값을 설정
2. weakSet
- 객체 중복불가
- 주어진 객체가 set안에 존재하는지 확인하는 용도
- 관련 메서드
1) WeakSet.prototype.delete(key) : delete되면 has할때 false 반환
2) WeakSet.prototype.get(key) : 관련값이 없으면 undefined 반환
3) WeakSet.prototype.has(key) : boolean 반환
4) WeakSet.prototype.set(key) : 객체 내 key에 대해 값을 설정
| let arr = [1,2,3,4]; let arr2 = [5,6,7,8]; let obj = [arr, arr2]; let ws = new WeakSet(); ws.add(arr); ws.add(arr2); ws.add(obj); arr = null; arr2 = null; console.log(ws.has(arr), ws.has(arr2)); |
| 결과 : false, true |
728x90
반응형