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: true
try {
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 |
๋ธ๋ผ์ฐ์ ํธํ์ฑ
Loadingโฆ