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

WeakMap - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

WeakMap

*

20157

*

WeakMap JavaScript WeakMap WeakMap WeakMap

WeakMap WeakMap WeakMap Map WeakMap

WeakMap WeakMap

WeakMap

WeakMap

JavaScript map API API

  1. O(n) n

WeakMap WeakMap

  • WeakMap

WeakMap

WeakMap Map

WeakMap()

WeakMap

WeakMap.prototype WeakMap

WeakMap.prototype.constructor

WeakMap WeakMap

WeakMap.prototype[Symbol.toStringTag]

[Symbol.toStringTag] "WeakMap" Object.prototype.toString()

WeakMap.prototype.delete()

key WeakMap.prototype.has(key) false

WeakMap.prototype.get()

key undefined

WeakMap.prototype.has()

WeakMap key

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); // value 
wm2.set(o2, undefined);
wm2.set(wm1, wm2); //  WeakMap 

wm1.get(o2); // "azerty"
wm2.get(o2); // undefined undefined
wm2.get(o3); // undefinedwm2  o3 

wm1.has(o2); // true
wm2.has(o2); // true undefined
wm2.has(o3); // false

wm3.set(o1, 37);
wm3.get(o1); // 37

wm1.has(o1); // true
wm1.delete(o1);
wm1.has(o1); // false

.clear() WeakMap

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

ECMAScript 2027 LanguageSpecification
# sec-weakmap-objects


Web Proxy Viewer  |  New URL  |  Original Page