| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/WeakMap | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.
* Some parts of this feature may have varying levels of support.
WeakMap / , JavaScript . WeakMap , WeakMap . , , . WeakMap , , .
WeakMap . WeakMap , WeakMap . WeakMap , . WeakMap Map .
WeakMap WeakMap .
JavaScript API 4 API ( , ) . . , . , .
.
O(n)(n ). ., WeakMap , . WeakMap .
WeakMap .WeakMap .
WeakMap . . , . Map .
WeakMap() WeakMap .
WeakMap.prototype WeakMap .
WeakMap.prototype.constructor . WeakMap WeakMap .
WeakMap.prototype[@@toStringTag]@@toStringTag "WeakMap". Object.prototype.toString() .
WeakMap.prototype.delete()key . WeakMap.prototype.has(key) false .
WeakMap.prototype.get()key . undefined .
WeakMap.prototype.has()key WeakMap .
WeakMap.prototype.set()WeakMap key value . WeakMap .
const wm1 = new WeakMap();
const wm2 = new WeakMap();
const wm3 = new WeakMap();
const o1 = {};
const o2 = function () {};
const o3 = window;
wm1.set(o1, 37);
wm1.set(o2, "azerty");
wm2.set(o1, o2); //
wm2.set(o3, undefined);
wm2.set(wm1, wm2); // . WeakMap !
wm1.get(o2); // "azerty"
wm2.get(o2); // undefined, wm2 o2
wm2.get(o3); // undefined, o3 undefined
wm1.has(o2); // true
wm2.has(o2); // false
wm2.has(o3); // true ( undefined)
wm3.set(o1, 37);
wm3.get(o1); // 37
wm1.has(o1); // true
wm1.delete(o1);
wm1.has(o1); // false
class ClearableWeakMap {
#wm;
constructor(init) {
this.#wm = new WeakMap(init);
}
clear() {
this.#wm = new WeakMap();
}
delete(k) {
return this.#wm.delete(k);
}
get(k) {
return this.#wm.get(k);
}
has(k) {
return this.#wm.has(k);
}
set(k, v) {
this.#wm.set(k, v);
return this;
}
}
WeakMap , .
Map , WeakMap , .Symbol , WeakMap Object.getOwnPropertySymbols .WeakMap , .let Thing;
{
const privateScope = new WeakMap();
let counter = 0;
Thing = function () {
this.someProperty = "foo";
privateScope.set(this, {
hidden: ++counter,
});
};
Thing.prototype.showPublic = function () {
return this.someProperty;
};
Thing.prototype.showPrivate = function () {
return privateScope.get(this).hidden;
};
}
console.log(typeof privateScope);
// "undefined"
const thing = new Thing();
console.log(thing);
// Thing {someProperty: "foo"}
thing.showPublic();
// "foo"
thing.showPrivate();
// 1
class Thing {
static #counter = 0;
#hidden;
constructor() {
this.someProperty = "foo";
this.#hidden = ++Thing.#counter;
}
showPublic() {
return this.someProperty;
}
showPrivate() {
return this.#hidden;
}
}
console.log(thing);
// Thing {someProperty: "foo"}
thing.showPublic();
// "foo"
thing.showPrivate();
// 1
WeakMap . , .
. , DOM . .
const buttons = document.querySelectorAll(".button");
buttons.forEach((button) => {
button.clicked = false;
button.addEventListener("click", () => {
button.clicked = true;
const currentButtons = [...document.querySelectorAll(".button")];
if (currentButtons.every((button) => button.clicked)) {
console.log(" !");
}
});
});
, .
clicked Object.keys(button), for...in . Object.defineProperty() , .clicked . Symbol , Object.getOwnPropertySymbols() .WeakMap .
const buttons = document.querySelectorAll(".button");
const clicked = new WeakMap();
buttons.forEach((button) => {
clicked.set(button, false);
button.addEventListener("click", () => {
clicked.set(button, true);
const currentButtons = [...document.querySelectorAll(".button")];
if (currentButtons.every((button) => clicked.get(button))) {
console.log(" !");
}
});
});
clicked . DOM .
, . ( , ) .
const cache = new WeakMap();
function handleObjectValues(obj) {
if (cache.has(obj)) {
return cache.get(obj);
}
const result = Object.values(obj).map(heavyComputation);
cache.set(obj, result);
return result;
}
. , . Map WeakRef , ( ) . WeakRef FinalizationRegistry .
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-weakmap-objects |
This page was last modified on 2025 7 24 by MDN contributors.
WeakMapObject/FunctionObject.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toString()Object.prototype.valueOf()Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |