| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/WeakMap | [Back] [Original] |
Get to know MDN better
WeakMap JavaScript WeakMap WeakMap WeakMap
WeakMapWeakMapWeakMap WeakMap Map
WeakMap WeakMap
Map SameValueZero === WeakMap
API 4 API 2 1 1 JavaScript
2
O(n) (n ) WeakMap WeakMap
WeakMap WeakMap
WeakMap Map
WeakMap() WeakMap
WeakMap.prototype WeakMap
WeakMap.prototype.delete()key WeakMap.prototype.has(key) false
WeakMap.prototype.get()key undefined
WeakMap.prototype.getOrInsert() WeakMap
WeakMap.prototype.getOrInsertComputed() WeakMap
WeakMap.prototype.has() WeakMap
WeakMap.prototype.set() WeakMap
const wm1 = new WeakMap();
const wm2 = new WeakMap();
const wm3 = new WeakMap();
const o1 = {};
const o2 = () => {};
const o3 = window;
wm1.set(o1, 37);
wm1.set(o2, "azerty");
wm2.set(o1, o2); //
wm2.set(o2, undefined);
wm2.set(wm1, wm2); // WeakMap
wm1.get(o2); // "azerty"
wm2.get(o2); // undefined undefined
wm2.get(o3); // o3 wm2 undefined
wm1.has(o2); // true
wm2.has(o2); // true 'undefied'
wm2.has(o3); // false
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 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
DOM 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
| ECMAScript 2027 LanguageSpecification # sec-weakmap-objects |
| Web Proxy Viewer | New URL | Original Page |