WeakSet 객체는 컬렉션 내 약하게 유지되는(held, 잡아두는) 객체를 저장할 수 있습니다.
구문
new WeakSet([iterable]);
매개변수
- iterable
- iterable 객체가 전달된 경우, 모든 객체 요소는 새로운
WeakSet에 추가됩니다. null은 undefined로 취급됩니다.
설명
WeakSet 객체는 객체 컬렉션입니다. WeakSet 내 객체는 오직 한 번만 발생할 수 있습니다. 즉, WeakSet 컬렉션 내에서 유일합니다.
Set 객체와 주된 차이는 다음과 같습니다:
Set과 달리,WeakSet은 객체 만의 컬렉션이며 모든 유형의 임의 값(의 컬렉션)은 아닙니다.WeakSet은 약합니다(weak): 컬렉션 내 객체 참조는 약하게 유지됩니다.WeakSet내 저장된 객체에 다른 참조가 없는 경우, 쓰레기 수집(garbage collection)될 수 있습니다. 이는 또한 컬렉션 내 현재 저장된 객체 목록이 없음을 뜻합니다.WeakSets은 열거불가입니다.
속성
WeakSet.lengthlength속성값은 0.WeakSet.prototypeSet생성자에 대한 프로토타입을 나타냅니다. 모든WeakSet객체에 속성을 추가할 수 있습니다.
WeakSet 인스턴스
모든 WeakSet 인스턴스는 WeakSet.prototype에서 상속합니다.
속성
WeakSet.prototype.constructor- 인스턴스의 프로토타입을 생성한 함수를 반환합니다. 이는 기본으로
WeakSet함수입니다.
메서드
WeakSet.prototype.add(value)WeakSet객체에 주어진 값을 갖는 새로운 객체를 추가합니다.WeakSet.prototype.delete(value)value와 관련된 요소를 제거합니다.WeakSet.prototype.has(value)는 그 뒤에false를 반환합니다.WeakSet.prototype.has(value)WeakSet객체 내 주어진 값을 갖는 요소가 존재하는 지 여부를 주장하는(asserting, 나타내는) boolean을 반환합니다.WeakSet.prototype.clear()WeakSet객체에서 모든 요소를 제거합니다.
예
WeakSet 객체 사용
var ws = new WeakSet();
var obj = {};
var foo = {};
ws.add(window);
ws.add(obj);
ws.has(window); // true
ws.has(foo); // false, foo가 집합에 추가되지 않았음
ws.delete(window); // 집합에서 window 제거함
ws.has(window); // false, window가 제거되었음
스펙
브라우저 호환성
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 36 | 34 (34) | No support | 23 | 9 |
new WeakSet(iterable) |
38 | 34 (34) | No support | 25 | 9 |
Constructor argument: new WeakSet(null) |
(Yes) | 37 (37) | ? | ? | 9 |
Monkey-patched add() in Constructor |
(Yes) | 37 (37) | ? | ? | 9 |
| Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| Basic support | No support | 34.0 (34) | No support | No support | 9 |
new WeakMap(iterable) |
No support | 34.0 (34) | No support | No support | 9 |
Constructor argument: new WeakSet(null) |
? | (Yes) | ? | ? | 9 |
Monkey-patched add() in Constructor |
? | (Yes) | ? | ? | 9 |