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

구문

new WeakSet([iterable]);

매개변수

iterable
iterable 객체가 전달된 경우, 모든 객체 요소는 새로운 WeakSet에 추가됩니다. null은 undefined로 취급됩니다.

설명

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

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

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

속성

WeakSet.length
length 속성값은 0.
WeakSet.prototype
Set 생성자에 대한 프로토타입을 나타냅니다. 모든 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가 제거되었음

명세

명세 상태 설명
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'WeakSet' in that specification.
Standard 초기 정의.
ECMAScript Latest Draft (ECMA-262)
The definition of 'WeakSet' in that specification.
Draft  

브라우저 호환성

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
WeakSetChrome Full support 36Edge Full support 12Firefox Full support 34IE No support NoOpera Full support 23Safari Full support 9WebView Android Full support 37Chrome Android Full support 36Firefox Android Full support 34Opera Android Full support 24Safari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 0.12
new WeakSet(iterable)Chrome Full support 38Edge Full support 12Firefox Full support 34IE No support NoOpera Full support 25Safari Full support 9WebView Android Full support 38Chrome Android Full support 38Firefox Android Full support 34Opera Android Full support 25Safari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 0.12
new WeakSet(null)Chrome Full support YesEdge Full support 12Firefox Full support 37IE No support NoOpera Full support YesSafari Full support 9WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 37Opera Android ? Safari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 0.12
addChrome Full support 36Edge Full support 12Firefox Full support 34IE No support NoOpera Full support 23Safari Full support 9WebView Android Full support 37Chrome Android Full support 36Firefox Android Full support 34Opera Android Full support 24Safari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 0.12
clear
DeprecatedNon-standard
Chrome No support 36 — 43Edge No support NoFirefox No support 34 — 46IE No support NoOpera No support 25 — 30Safari No support NoWebView Android No support 37 — 43Chrome Android No support 36 — 43Firefox Android No support 34 — 46Opera Android No support 25 — 30Safari iOS No support NoSamsung Internet Android Full support Yesnodejs No support No
deleteChrome Full support 36Edge Full support 12Firefox Full support 34IE No support NoOpera Full support 23Safari Full support 9WebView Android Full support 37Chrome Android Full support 36Firefox Android Full support 34Opera Android Full support 24Safari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 0.12
hasChrome Full support 36Edge Full support 12Firefox Full support 34IE No support NoOpera Full support 23Safari Full support 9WebView Android Full support 37Chrome Android Full support 36Firefox Android Full support 34Opera Android Full support 24Safari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 0.12
prototypeChrome Full support 36Edge Full support YesFirefox Full support 34IE No support NoOpera Full support 23Safari Full support 9WebView Android Full support 37Chrome Android Full support 36Firefox Android Full support 34Opera Android Full support 24Safari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 0.12

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.

같이 보기

문서 태그 및 공헌자

이 페이지의 공헌자: alattalatta, mdnwebdocs-bot, Netaras, fscholz
최종 변경자: alattalatta,