/
spirzen
/
it-code-examples
Обзор
Документация
Войти
/
spirzen
/
it-code-examples
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
examples/javascript/js-501-22-049/main.js
31 строка
780 B
Spirzen
Code migration pack
09 июн 2026, 01:41
09 июн 2026, 01:41
cebc284
Код
Авторство
О чём код?
class CacheItem { constructor(value) { this.value = value; this.timestamp = Date.now(); } } const cache = new Map(); function addToCache(key, value) { const item = new CacheItem(value); cache.set(key, new WeakRef(item)); return item; } function getFromCache(key) { const ref = cache.get(key); if (ref) { return ref.deref(); } return undefined; } const obj = addToCache("user1", { name: "Иван" }); console.log(getFromCache("user1")); // CacheItem { value: { name: "Иван" }, timestamp: ... } // После удаления сильной ссылки объект может быть собран obj = null; setTimeout(() => { console.log(getFromCache("user1")); // undefined (возможно) }, 100);