WeakSet

WeakSet 객체는 약하게 유지되는(held, 잡아두는) 객체를 컬렉션에 저장할 수 있습니다.

설명

WeakSet 객체는 객체 컬렉션입니다. WeakSet 내 객체는 오직 한 번만 발생할 수 있습니다. 즉, WeakSet 컬렉션 내에서 유일합니다.

Set 객체와 주된 차이는 다음과 같습니다:

  • Set과 달리, WeakSet객체 만의 컬렉션이며 모든 유형의 임의 값(의 컬렉션)은 아닙니다.
  • WeakSet은 약합니다(weak): 컬렉션 내 객체 참조는 약하게 유지됩니다. WeakSet 내 저장된 객체에 다른 참조가 없는 경우, 쓰레기 수집(garbage collection)될 수 있습니다. 이는 또한 컬렉션 내 현재 저장된 객체 목록이 없음을 뜻합니다. WeakSets은 열거불가입니다.

생성자

WeakSet()

Creates a new WeakSet object.

인스턴스 메서드

WeakSet.prototype.add(value) (en-US)

Appends value to the WeakSet object.

WeakSet.prototype.delete(value) (en-US)

Removes value from the WeakSet. WeakSet.prototype.has(value) will return false afterwards.

WeakSet.prototype.has(value) (en-US)

Returns a boolean asserting whether value is present in the WeakSet object or not.

예제

WeakSet 객체 사용

js
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가 제거되었음

명세

Specification
ECMAScript Language Specification
# sec-weakset-objects

브라우저 호환성

BCD tables only load in the browser

같이 보기