Does JavaScript have an equivalent to Perl DESTROY?
Is there any method that gets called or an event that is dispatched just before the 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 memory is freed
My target platform is Firefox (and I don't need to support other browsers), so if there is only a specific Firefox way, that's okay.
And a little background: I'm writing a Perl module XUL :: Gui that serves as a bridge between Perl and Firefox, and I'm currently working on plugging in a few DOM Elements-related memory leaks that stick out forever, even after they're gone. and there are no more links left on the Perl side. So I'm looking for ways to figure out when JavaScript elements are destroyed, or a way to force JavaScript to clear the object.
If there is no way to do this in pure JavaScript, XPConnect / XPCOM or any other special Mozilla technology is acceptable.
a source to share
Does XUL :: Gui support browser interaction at the SpiderMonkey API level? If so, https://developer.mozilla.org/en/SpiderMonkey/JSAPI_Reference/JSClass.finalize might be helpful for you. Otherwise, you might get stuck as, as Matthew Flashen says, there is no way to do this in Javascript.
a source to share