title: JS Is it possible to access function variable?
body: <p>My understanding is that with JavaScript a "function", is a function, but can also be a constructor, and an object itself. If I design a function like a function (with a return), but also like a constructor, can I ever have access to both the return value, and that property outside of the function scope? Like so</p>
  
  <pre><code>function Tree() {
      this.name = "Boris";
      var age = 18
      var cookies = "cookies"    // defined with var instead of this.cookies
  
      return age;
  }
  
  var r1 = Tree();       // r1 == 18
  r1.name                // undefined
  var r2 = new Tree();   // r2 == { name: "Boris" }, but no age or cookies
  </code></pre>
  
  <p>So can I ever access name, age, and cookies with the same object? The more and more I think this over, what I want to do probably isn't possible because of security reasons, but I still would like to know.</p>
  
  <p>Using <code>obj.constructor()</code> you once again have access to the function you used to create the obj, and thus the return value. Note: prefixing with <code>new</code> will return another object, not the return value.</p>
  
  <pre><code> var r2 = new Tree();   // r2 == { name: "Boris" }
   r2.constructor()       // 18
   new r2.constructor()   // { name: "Boris" }, not what I want
  </code></pre>
  
  <p>I couldn't find a way to access variables defined with var. Help?</p>