2

If I have a self-destroying function

function tempFunc() {
   //do some stuff, then...

   tempFunc = function() {return;}
}

or

function tempFunc() {
   //do some stuff, then...

   delete tempFunc;
}

What happens to the original code of tempFunc? Is it held in memory anywhere? How is the situation changed if the function leaves behind something a bit more permanent e.g. creates an object which has access to variables contained within the function's closure.

wheresrhys
  • 22,558
  • 19
  • 94
  • 162
  • 1
    I don't think javascript (or ECMA script) specifies how memory is reclaimed, if at all. Most probably, your implementations keeps tabs on what's reachable and does a garbage collection. – falstro Feb 14 '11 at 14:10
  • possible duplicate of [What is JavaScript garbage collection?](http://stackoverflow.com/questions/864516/what-is-javascript-garbage-collection) – falstro Feb 14 '11 at 14:12

1 Answers1

0

When there are no more referenced to the function, it can be garbage-collected.

If there are referenced through closures, it can't.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • In addition : http://stackoverflow.com/questions/864516/what-is-javascript-garbage-collection :) – stecb Feb 14 '11 at 14:10