title: Is it a way to capture the stack of closure definition?
body: <p>I'm currently researching a way to protect JavaScript functions as <em>private</em>, exception will be thrown if the caller is invalid. Then I have a problem here: If a closure is defined inside a valid caller, is it a way to get the valid caller from the inner closure?</p>
  
  <p>For example:</p>
  
  <pre><code>function validCaller() {
      var self = this;
      self.privateFunction1();
      $.each([/*...*/], function (key, val) {
          self.privateFunction2(); 
      });
  }
  </code></pre>
  
  <p>And, of course, <code>privateFunction1</code> will get the caller <code>validCaller</code> correctly, but <code>privateFunction2</code> will get the anonymous function as caller, so the access will be denied.</p>
  
  <p>It is not the right way if I get validCaller from the call stack by expression <code>caller.caller.caller</code>, because I can't determine how many levels should I trace back, and, in some special cases, the anonymous function needs to be called outside of <code>validCaller</code>.</p>
  
  <p>Maybe I asked the question in a complicated way, the simple way is: </p>
  
  <blockquote>
    <p><strong>how can I capture a function's scope chain?</strong></p>
  </blockquote>