Does JavaScript have an equivalent to Perl's DESTROY method?
- by Eric Strom
Is there any method that is called or event that is dispatched right before an Element is cleaned up by the JavaScript garbage collector?
In Perl I would write:
package MyObj;
sub new {bless {}}
sub DESTROY {print "cleaning up @_\n"}
and then later:
{
my $obj = MyObj->new;
# do something with obj
} # scope ends, and assuming there are no external references to $obj,
# the DESTROY method will be called before the object's memory is freed
My target platform is Firefox (and I have no need to support other browsers), so if there is only a Firefox specific way of doing this, that is fine.
And a little background: I am writing the Perl module XUL::Gui which serves as a bridge between Perl and Firefox, and I am currently working on plugging a few memory leaks related to DOM Elements sticking around forever, even after they are gone and no more references remain on the Perl side. So I am looking for ways to either figure out when JavaScript Elements will be destroyed, or a way to force JavaScript to cleanup an object.