admin管理员组文章数量:1321607
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.
- 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
4 Answers
Reset to default 4You 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);
本文标签: How to manually destroy an object in javascriptStack Overflow
版权声明:本文标题:How to manually destroy an object in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742096953a2420608.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论