title: Javascript Variable Scope. How Do I Access an Object Var From an Internal Object Function?
body: <p>I'm wondering how to access the "nam"e variable with a function in a javascript object like so:</p>
  
  <pre><code>function myObj() {
      this.vars = {
          name: 'bob',
          age: '42'
      }
      this.functions = {
          sayName: function() {
              alert(this.vars.name);
          }
      }
  }
  
  var obj = new myObj();
  obj.functions.sayName();
  </code></pre>
  
  <p>What am I missing here?</p>
  
  <p>I can get the name var if I pass a reference to itself like:</p>
  
  <pre><code>function myObj() {
      this.vars = {
          name: 'bob',
          age: '42'
      }
      this.functions = {
          sayName: function(name) {
              alert(name);
          }
      }
  }
  
  var obj = new myObj();
  obj.functions.sayName(obj.vars.name);
  </code></pre>
  
  <p>but that seems a little redundant... thoughts?</p>