function ConstructMe(msg) {
this.message = msg;
}
instances = {
instance1: new ConstructMe('msg1'),
instance2: new ConstructMe('msg2'),
instance3: new ConstructMe('msg3'),
instance4: new ConstructMe('msg4'),
instance5: new ConstructMe('msg5')
}
If I have this Constructor, and I build instances of it in an object (as shown above), how would I go about destroying them later on? I want to make sure that they are no longer accessible, but also to make sure they are no longer somewhere uselessly.
Would deleting the object work? Or the instances would just remain somewhere on the memory nameless?
In my case, I create many instances along certain actions in an app, I want to make sure I keep the memory clean and don't leave things hanging around, cluttering...
Looking forward to your feedback
Javascript does not work that way. It is a garbage collected language. Technically, you can do something like:
delete instances.instance3;
which should remove the property from the instances
object, but in practice you rarely need to worry about it. The JavaScript runtime will take care of cleaning up after you*
*Technically you can still create memory leaks in javascript, but in the vast majority of cases you won't need to worry about it.