[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/WeakMap [Back]  [Original]

WeakMap - JavaScript | MDN

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

View in English Always switch to English

WeakMap

Baseline Widely available *

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 .

In this article

WeakMap . . .

WeakMap?

JavaScript API 4 API ( , ) . . , . , .

.

  1. O(n)(n ). .
  2. . , . .

, 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 .

WeakMap

js
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

WeakMap .clear()

js
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 , .

js
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

.

js
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 . .

js
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("  !");
    }
  });
});

, .

WeakMap .

js
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 .

, . ( , ) .

js
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


Web Proxy Viewer  |  New URL  |  Original Page