最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to manually destroy an object in javascript? - Stack Overflow

programmeradmin7浏览0评论

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.

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.

Share Improve this question edited Oct 25, 2017 at 11:27 kalpa asked Oct 25, 2017 at 11:06 kalpakalpa 6973 gold badges11 silver badges23 bronze badges 3
  • This really sounds like an XY Problem - "How to force an object out of scope?" Just let it fall out of scope. Have you got an underlying issue you're trying to solve here? – James Thorpe Commented Oct 25, 2017 at 11:11
  • @JamesThorpe Actually The thing I wanted to achieve is a class having a static method destroy to destroy the instances of those objects. 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. Thanks. – kalpa Commented Oct 25, 2017 at 11:19
  • You may use like=> abcObj = null; delete abcObj; /* First set the pointer to null, then deleting the object */ – user3419778 Commented Oct 25, 2017 at 11:55
Add a ment  | 

4 Answers 4

Reset to default 4

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.

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);

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.

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);

发布评论

评论列表(0)

  1. 暂无评论