简体   繁体   中英

Javascript - how to delete an object?

I have an object:

var myObj = new myAwsumObj();

Now, currently I am deleting it like that:

myObj = undefined;

But that apparently does nothing, since object still exists (I can see that exceptions are being thrown by something that is in the 'external' javascript file that this object is defined in).

How do I clear all this stuff up? Is it possible? Because it seems like myObj is doing very much in this external javascript file, maybe even creating new objects, is it possible to clear all this mess without refactoring this external file?

Unfortunately, it's not possible. From your description, the object itself seems to be creating references to itself, by adding event handlers, setting timeouts/intervals, ajax callbacks, whatever. Even if you clear all the references to the object created by yourself, you'd still have to clear the references it creates on its own. Only then it would become inactive, unreferenced, and eligible for garbage collection.

But look into its source code, maybe the object provides a "destruct" method to take care of all that mess.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM