1

I just read that local variable lives only until the function in which it is declared ends.

Thank you all for answers. My main problem was that those two statements contradict each other. So which one is true? I guess the contradiction is only superficial.

I would guess the garbage collector would wipe the "closed" variable...

Well now, sorry for being so verbose, I guess I need to practice more the rubber duck techniqe.

I just realised that we declare a local variable in closure and then the garbage collector does not wipe the variable since we hold the reference from the returned object, right?

So? Memory leaks? Any option to check the list of such variables and clean them?

so_user
  • 333
  • 3
  • 15
  • 2
    [What is garbage collection](http://stackoverflow.com/questions/864516/what-is-javascript-garbage-collection) – adeneo Jan 24 '16 at 00:15
  • 1
    @adeneo it's where a guy comes around in a truck once a week to remove old tv's and beer cans? – charlietfl Jan 24 '16 at 00:31

3 Answers3

2

You want to know how that functionality is implemented internally? Different js engines could use different strategies there. What matters is what the language behaves like.

Brendan Eich - the creator of JavaScript - was a big fan of Scheme and therefore added closures to JS that behave exactly like Scheme closures. One of the best explanations of how closures work is given by one of Scheme's creators in a series of lectures he gave in 1986, called "structure and interpretation of computer programs", specifically lecture 7A and 7B where he talks about writing an interpreter and explains how arguments and local variables are stored in the "environment" over which a closure closes. btw. the term "environment" is used differently nowadays, at least in the JavaScript world but closures still work the same way they did back then.

Community
  • 1
  • 1
Tesseract
  • 8,049
  • 2
  • 20
  • 37
0

Open it's scope.

Sample 1

function() {
   var localVarThatWouldGo = "hi";
   // set to window scope
   window.myVar = localVarThatWouldGo;
}

But setting global scoped variables is not good practice. Instead, set it to the scope you need. For example if your application is in a view model like so:

Sample 2

function runApp() {
    // point to this object
    var self = this;

    this.myFunction = function() {
          self.scopedVariable = "hi"; // this won't go for the life time of the app variable below
    }
}
var app = new runApp()
Fred Johnson
  • 2,539
  • 3
  • 26
  • 52
0

Closures are a really just a neat compiler trick. When the compiler detects a situation in which a variable is "closed over" (a variable is used lexically in a nested scope), it generates a new object under the hood for you and assigns your local variable to a field on the generated object. You as a developer never need to worry about accessing that variable as generatedObject.yourVariable, the compiler essentially aliases this for you.

var getTimeSinceStartUp = (function() {
   var time = new Date();    //time is 'closed over', it 'leaks' into the sub scope
   return function() {
    return new Date() - time; //time is available here because of the hidden type the compiler made for you.
  });
})();
var ms = getTimeSinceStartup();
weichsem
  • 278
  • 1
  • 6