kalpa
kalpa

Reputation: 697

How to manually destroy an object in javascript?

Consider the below code,

function destroyer(obj){
  obj = undefined;
}

abcObj = {
  a:1,
  b:2
}

destroyer(abcObj);

As far as I understand, all I can do is throw the object out of scope and the GC will clean it when it sees fit. But the above code does not throw the object out of scope.

How to force an object out of scope?

Reason for wanting to do so: The thing I wanted to achieve is a class having a static method destroy to destroy the instances of same class. Is that possible in javascript? Or is it that I can't force the cleanup. A cleanup method would be great since, the lib I'm working with spawns a lot of instances like 200 to 300.

Upvotes: 1

Views: 10411

Answers (4)

Guillaume Georges
Guillaume Georges

Reputation: 4020

You could pass your variable in an object, as a key, and pass that object to your destroyer function. That way, your function could use the object's first key to retrieve the global variable's name, and then delete it in the window object.

Please note this will only work for global variables.

// retrieves the first key of the object passed as parameter
function getFirstKeyName(someObject){
  return Object.keys(someObject)[0];
}

// destroy a global variable by its name
function destroyGlobalVariable(withinAnObject){
  // retrieve the variable's name
  var globalVarName = getFirstKeyName(withinAnObject);
  console.log("Deleting global variable " + globalVarName);
  // use delete, assign undefined or null to window[globalVarName]; 
  delete window[globalVarName];
}

abcObj = {
  a:1,
  b:2
}

// abcObj should be defined : 
console.log(abcObj);

// pass your variable name into an object, as a key
destroyGlobalVariable({abcObj});

// abcObj should not be defined now : 
console.log(abcObj);

Upvotes: 0

tevemadar
tevemadar

Reputation: 13225

There is delete in JavaScript, but it does not delete objects, it deletes properties from objects. For your particular use case it could be used:

function destroyer(name){
    delete window[name];
}

test="hello";
console.log(test);
console.log("before destroy");
destroyer('test');
console.log("after destroy");
console.log(test);

It works because you have a global variable, and "global" scope is actually "window" in the browser.

However in the general case the most you could do is to explicitly put your objects into some container object, and then you can remove them from there. It does not worth the effort IMHO.

EDIT: actually it does not work as a code snippet here, because it has its own "global-ish" scope, which is not window. But you can test it in the developer console of a browser.

EDIT2: I was wrong, it works here too, just I got confused because of the lengthy error message. Added log-lines before and after the destroyer-call.

Upvotes: 0

Zakaria Acharki
Zakaria Acharki

Reputation: 67525

Since it's defined globally just destroy it directly :

function destroyer(){
  abcObj = undefined;
}

abcObj = {
  a:1,
  b:2
}

console.log(abcObj);

destroyer();

console.log(abcObj);

Upvotes: 2

Quentin
Quentin

Reputation: 944294

You have to modify all the variables pointing to it.

abcObj = null;

You can't use a function to do it because that would copy the value to a new variable and leave the original untouched.


This is probably pointless as you are unlikely to be creating a significant number of objects without them falling out of scope naturally. 99.9% of the time you can just focus on writing code which does what you need it to do and let the JS engine worry about garbage collection in its own time.

Upvotes: 4

Related Questions