title: What is a practical use for a closure in JavaScript?
body: <p>I'm <a href="http://jsbin.com/ojuxo/edit" rel="noreferrer">trying</a> my hardest to wrap my head around JavaScript closures.</p>
  
  <p>I get that by returning an inner function, it will have access to any variable defined in its immediate parent.</p>
  
  <p>Where would this be useful to me? Perhaps I haven't quite got my head around it yet. Most of the <a href="http://blog.morrisjohns.com/javascript_closures_for_dummies.html" rel="noreferrer">examples I have seen online</a> don't provide any real world code, just vague examples.</p>
  
  <p>Can someone show me a real world use of a closure?</p>
  
  <p>Is this one, for example?</p>
  
  <pre><code>var warnUser = function (msg) {
      var calledCount = 0;
      return function() {
         calledCount++;
         alert(msg + '\nYou have been warned ' + calledCount + ' times.');
      };
  };
  
  var warnForTamper = warnUser('You can not tamper with our HTML.');
  warnForTamper();
  warnForTamper();
  </code></pre>