class Cat {
storage = new Map()
constructor(id) {
if(storage.has(id)) return storage.get(id)
storage.set(id, this)
}
}
I want the object to be removed from the storage if references to it are not used in the application. But if the links in the application are exists, and we are trying to create an object with the same ID, then return this object, rather than create a new one. How i can do it without destructors?
But when all the references to the object disappear from the application, and object removed from the storage, then there is nothing bad to create a new instance of the object
Javascript not support this feature. I came up with a workaround:
At each object constructing, we increase the number of links by one, and with each destructuring we reduce the number of links by one. and when the number of links is zero, we manually delete the object from the storage.
usage: