title: Scope within an anonymous function?
body: <p>I'm coming from an Actionscript background and (very late to the party) I am trying to learn JavaScript. I am going through this <a href="https://www.youtube.com/watch?v=R-lPviKS_WM" rel="noreferrer">AngularJS - Video Tutorial For Beginners</a> on YouTube (it's pretty good) and saw something really basic that I don't understand.</p>
  
  <p>At line 5 the <code>var workcount</code> is defined. Then two anonymous functions are defined and returned in an object. The functions reference <code>workcount</code> but isn't <code>workcount</code> in a different scope? Is this like <code>blocks</code> in Objective-C where local vars remain accessible within the block. Is there a name for what this is?</p>
  
  <p><strong>Or</strong> if a function "knows about" vars previously defined in its scope, would the function <code>task2</code> "know about" <code>task1</code>?</p>
  
  <p>It bugs me that I can't make sense of this.</p>
  
  <p><strong>Update:</strong> Thanks for all the replies. I get it now – and while I have seen the term "closures" before, I never understood it (it seems a not very descriptive term. In reading up, I saw the term "stack-frames" and then the light bulb lit up: stack... frame of reference);</p>
  
  <hr>
  
  <pre><code>var createWorker = function(){
  
    var workCount = 0;
  
    var task1 = function(){
      workCount += 1;
      console.log("task1" , workCount);
    };
  
    var task2 = function(){
      workCount += 1;
      console.log("task2" , workCount);
    };
  
    return {
      job1: task1,
      job2:task2
    }
  };
  
  worker=createWorker();
  
  worker.job1();
  
  worker.job2();
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>task1 1
  task2 2
  </code></pre>