WeakSet() 생성자

WeakSet() 생성자는 WeakSet 객체를 생성합니다.

구문

js
new WeakSet()
new WeakSet(iterable)

참고: WeakSet()은 오직 new로만 생성할 수 있습니다. new 없이 호출을 시도하면 TypeError가 발생합니다.

매개변수

iterable Optional

만약 순회 가능한 객체가 매개변수로 주어진다면, 모든 해당 객체가 새로운 WeakSet에 추가됩니다. nullundefined로 취급합니다.

예제

WeakSet 객체 사용하기

js
const ws = new WeakSet();
const foo = {};
const bar = {};

ws.add(foo);
ws.add(bar);

ws.has(foo); // true
ws.has(bar); // true

ws.delete(foo); // foo 를 set에서 제거
ws.has(foo); // false, foo는 이미 삭제되었습니다
ws.has(bar); // true, bar는 유지 중입니다

명심하실 점은 foo !== bar라는 점입니다. 둘은 서로 비슷한 객체지만 같은 객체가 아닙니다. 그래서 두 객체 모두 set에 추가 가능합니다.

명세서

Specification
ECMAScript Language Specification
# sec-weakset-constructor

브라우저 호환성

BCD tables only load in the browser

같이 보기