Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: Create and destroy class instance through class method

I'm trying to figure out how to delete an object through a class method. I would like to be able to create a class that has a destroy method that releases the object from memory. So far, the research I've done has been inconclusive. I understand that garbage collection will eventually take care of the object, but I'd like a more definitive way to destroy it. Is this possible?

// class constructor var class = function () {      this.destroy = function () {}; };  // instance var instance = new class(); instance.destroy(); console.log(instance); // should be null or undefined 
like image 706
user699242 Avatar asked Jan 14 '14 16:01

user699242


People also ask

How to destroy an instance of a class JavaScript?

instance = new Class(); //since 'storage. instance' is your only reference to the object, whenever you wanted to destroy do this: storage. instance = null; // OR delete storage.

How do you destroy a class instance?

To delete an instance, in JavaScript, you remove all references pointing to it, so that the garbage collector can reclaim it. This means you must know the variables holding those references. You can only delete a variable ( x here) if it's declared without var ...

How to destroy object in js?

You can't delete objects, they are removed when there are no more references to them. You can delete references with delete . However, if you have created circular references in your objects you may have to de-couple some things.

How do I delete a class in node JS?

To remove a class from an element, you use the remove() method of the classList property of the element.

How to destroy an object in JavaScript?

1- There is no way to actually destroy an object in javascript, but using delete, we could remove a reference from an object:

How to create a class in JavaScript?

JavaScript Classes are templates for JavaScript Objects. Use the keyword class to create a class. Always add a method named constructor (): constructor () { ... } The example above creates a class named "Car". The class has two initial properties: "name" and "year".

What is the use of class in JavaScript?

It is a template for JavaScript objects. The example above uses the Car class to create two Car objects. The constructor method is called automatically when a new object is created. If you do not define a constructor method, JavaScript will add an empty constructor method. Class methods are created with the same syntax as object methods.

How do I create a class with multiple methods?

Class methods are created with the same syntax as object methods. Use the keyword class to create a class. Always add a constructor () method. Then add any number of methods. constructor () { ...


2 Answers

1- There is no way to actually destroy an object in javascript, but using delete, we could remove a reference from an object:

var obj = {}; obj.mypointer = null; delete obj.mypointer; 

2- The important point about the delete keyword is that it does not actually destroy the object BUT if only after deleting that reference to the object, there is no other reference left in the memory pointed to the same object, that object would be marked as collectible. The delete keyword deletes the reference but doesn't GC the actual object. it means if you have several references of the same object, the object will be collected just after you delete all the pointed references.

3- there are also some tricks and workarounds that could help us out, when we want to make sure we do not leave any memory leaks behind. for instance if you have an array consisting several objects, without any other pointed reference to those objects, if you recreate the array all those objects would be killed. For instance if you have var array = [{}, {}] overriding the value of the array like array = [] would remove the references to the two objects inside the array and those two objects would be marked as collectible.

4- for your solution the easiest way is just this:

var storage = {}; storage.instance = new Class(); //since 'storage.instance' is your only reference to the object, whenever you wanted to destroy do this: storage.instance = null; // OR delete storage.instance; 

As mentioned above, either setting storage.instance = null or delete storage.instance would suffice to remove the reference to the object and allow it to be cleaned up by the GC. The difference is that if you set it to null then the storage object still has a property called instance (with the value null). If you delete storage.instance then the storage object no longer has a property named instance.

and WHAT ABOUT destroy method ??

the paradoxical point here is if you use instance.destroy in the destroy function you have no access to the actual instance pointer, and it won't let you delete it.

The only way is to pass the reference to the destroy function and then delete it:

// Class constructor var Class = function () {      this.destroy = function (baseObject, refName) {          delete baseObject[refName];      }; };  // instanciate var storage = {}; storage.instance = new Class(); storage.instance.destroy(object, "instance"); console.log(storage.instance); // now it is undefined 

BUT if I were you I would simply stick to the first solution and delete the object like this:

storage.instance = null; // OR delete storage.instance; 

WOW it was too much :)

like image 68
Mehran Hatami Avatar answered Sep 21 '22 04:09

Mehran Hatami


No. JavaScript is automatically garbage collected; the object's memory will be reclaimed only if the GC decides to run and the object is eligible for collection.

Seeing as that will happen automatically as required, what would be the purpose of reclaiming the memory explicitly?

like image 28
Jon Avatar answered Sep 20 '22 04:09

Jon