Movatterモバイル変換


[0]ホーム

URL:


  1. 개발자를 위한 웹 기술
  2. JavaScript
  3. JavaScript 참고서
  4. 표준 내장 객체
  5. WeakSet
  6. WeakSet.prototype.add()

This page was translated from English by the community.Learn more and join the MDN Web Docs community.

View in EnglishAlways switch to English

WeakSet.prototype.add()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015년 9월⁩.

WeakSet 인스턴스의add() 메서드는 이WeakSet의 끝에 새로운 객체를 추가합니다.

시도해 보기

const weakset1 = new WeakSet();const object1 = {};weakset1.add(object1);console.log(weakset1.has(object1));// Expected output: truetry {  weakset1.add(1);} catch (error) {  console.log(error);  // Expected output (Chrome): TypeError: Invalid value used in weak set  // Expected output (Firefox): TypeError: WeakSet value must be an object, got 1  // Expected output (Safari): TypeError: Attempted to add a non-object key to a WeakSet}

구문

js
add(value)

매개변수

value

반드시 객체이거나등록되지 않은 심볼이어야 합니다.WeakSet 컬렉션에 추가될 값입니다.

반환 값

WeakSet 객체.

예외

TypeError

value가 객체가 아니거나등록되지 않은 심볼일 경우 발생합니다.

예제

add 사용하기

js
const ws = new WeakSet();ws.add(window); // window 객체를 WeakSet에 추가ws.has(window); // true// WeakSet는 인수로 오직 객체만 받습니다ws.add(1);// Chrome 에서는 "TypeError: Invalid value used in weak set"라는 결과가,// Firefox 에서는 "TypeError: 1 is not a non-null object" 라는 결과가 나옵니다

명세서

Specification
ECMAScript® 2026 Language Specification
# sec-weakset.prototype.add

브라우저 호환성

같이 보기

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp