0
(function(){
  var a;

  function inner1(arg){
    a = arg;
  }

  function inner2(){
    alert(a);
  }

})();

Will this cause memory leak in my application, since i am declaring variable a outside my other two inner functions.

Kevin
  • 23,174
  • 26
  • 81
  • 111
  • 3
    This is called [lexical scope](http://stackoverflow.com/q/1047454/1331430), aka JS's scope chain, one of the most common aspects of the language. All JS implementations have a GC (garbage collector) that will deal with memory issues by removing no longer referenced objects from the memory. You usually don't have a problem with "memory leaks" except when you have circular references, remove elements that still have attached handlers, and some IE bugs. – Fabrício Matté Jan 19 '13 at 16:00
  • Since this example function does *nothing*, there will be no leaking data. Garbage collection will depend on what *consisting* functions still reference your data – Bergi Jan 19 '13 at 16:16

1 Answers1

2

No, because you are declaring that variable inside an anonymous function closure already.

You can prove this by doing the following.

(function(){
  var a;

  function inner1(arg){
    a = arg;
  }

  function inner2(){
    alert(a);
  }

})();

alert(a)
Owen Allen
  • 11,348
  • 9
  • 51
  • 63