Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly dereference then delete a JavaScript Object?

I would like to know the correct way to completely dereference a JavaScript Object from memory. To ensure it's deletion without it dangling in memory, and that the garbage collector removes the object.

When I looked and this question Deleting Objects in JavaScript. It was explained that if you delete all the references of object, the GC will remove it from memory. I want to know how do I go about removing references from an object that has both methods and properties.

Suppose you have and object that was created by using function, and the object has both methods and properties. Say it looks something like this:

function myObject(x, y) {
   this.x = x; 
   this.y = y;

   this.myMethod = function() {
      // method code
   }
}

var myInstance = new myObject(24, 42) // How to remove this completely?

Note: This is an example, I understand you can use prototype for methods.

I know I can't just say delete myInstance. So in this case to completely delete the object will I need to call delete on all it's properties, and then call delete on the instance, like so?

delete myInstance.x;
delete myInstance.y;
delete myInstance; // I'm not sure if this is necessary.

Would this work? Or do I need to also delete it's methods (and if so how)?

Or perhaps there is a better and simpler way to do this?

like image 806
Spencer Wieczorek Avatar asked Aug 23 '14 01:08

Spencer Wieczorek


People also ask

How can we delete an object in JavaScript?

The only way to fully remove the properties of an object in JavaScript is by using delete operator. If the property which you're trying to delete doesn't exist, delete won't have any effect and can return true.

How do you dereference an object?

To access the contents of the data object to which a data reference is pointing, you must dereference it. ASSIGN dref->* TO <fs> [CASTING ...]. This statement assigns the data object to the field symbol <fs> which the data reference in the reference variable <dref> points to.

How does delete work in JavaScript?

The delete operator removes a property from an object. If the property's value is an object and there are no more references to the object, the object held by that property is eventually released automatically.

How do I delete an object in TypeScript?

To remove a property from an object in TypeScript, mark the property as optional on the type and use the delete operator. You can only remove properties that have been marked optional from an object. Copied!


1 Answers

Javascript is a garbage collected language. It will clean up an object ONLY when there is no other code that has a reference to it. Those other references need to either go out of scope (and not be held by a closure) or you can set those other variables to something else so they don't point at your object. When there are no other variables with a reference to your object, it will be automatically taken care of by the garbage collector, including any properties it has (assuming none of those properties are themselves objects that something has a reference to - but even then the host object would be cleaned up and only the object in the property would continue to live on).

You cannot delete an object any other way in Javascript.

So, to remove the object created by this:

var myInstance = new myObject(24, 42) // How to remove this completely?

Just clear myInstance like this:

myInstance = null;

You don't need to manually delete the properties from myInstance at all. If nobody has a reference to the mother object and none of the properties are objects that someone has a reference to, then the garbage collector will just clean everything up for you.

The delete operator is primarily for removing properties from an object when you want the mother object to remain (e.g. when you just want to remove a property).

like image 94
jfriend00 Avatar answered Oct 20 '22 15:10

jfriend00