On Aug 2, 8:19*am, Gregor Kofler <use...@gregorkofler.atwrote:
What is the best practice for removing anonymous functions?
Leave them for the garbage collector. Do you attempt to explicitly
remove named functions?
It seems to me that:
var foo = function(){ /*foo body */ };
foo();
foo = null;
results in the function formerly referred to as foo becoming anonymous
and, if there are no other references to it, it becomes available for
garbage collection.
It is, more or less, equivalent to:
(function(){ /*foo body */ })();
provided foo isn't called from elsewhere before being set to null.
Something like
(function() { doSomething(); arguments.callee = null; })();
seems to work (at least it triggers no errors or exceptions on FF, but
is this really a working solution?
It depends on what you interpret from the phrase "seems to work". The
arguments object belongs on to the execution context, it is given a
'callee' property that refers to the anonymous Function so that it can
be recursive. It has the property DontEnum, but is not specified as
being ReadOnly so you can set it to anything you like without causing
an error (in a browser conforming to ECMA-262, section 10.1.8).
Setting arguments.callee as a reference to some arbitrary object
doesn't seem to do anything useful and should have zero effect on when
the function is made available for garbage collection.
--
Rob