title: Javascript, inner classes, and how to access parent scope efficiently
body: <p>In Javascript, I would like to define a class with an inner (or nested) class.  Within the inner class I'd like to be able to get access to the parent instance. How can I do this efficiently? </p>
  
  <p>Some code will show what I mean. Suppose I define a class, MyType1, which exposes several properties and one function, <code>SayHello</code>:</p>
  
  <pre><code>(function(){
      MyType1 = function(name){
          this.TypeName = "MyType1";
          this.Name = name;
      };
  
      MyType1.prototype.SayHello = function() {
          say(this.Name + " says hello...");
      };
  })();
  </code></pre>
  
  <p>Ok, now, starting from there, I want to add an "inner class" to MyType1, so I add some new code so that it looks like this: </p>
  
  <pre><code>(function(){
      MyType1 = function(name){
          this.TypeName = "MyType1";
          this.Name = name;
          var parentName = name;
          this.Child = function(name) {
              this.Description = parentName + "'s child, " + name;
          };
  
          this.Child.prototype.Introduce = function() {
              say(this.Description + ", greets you...");
          };
      };
  
      MyType1.prototype.SayHello = function() {
          say(this.Name + " says hello...");
      };
  })();
  </code></pre>
  
  <p>Now I can use these classes like this: </p>
  
  <pre><code>var x = new MyType1("Victor");
  x.SayHello();
  
  var c = new x.Child("Elizabeth");
  c.Introduce();
  </code></pre>
  
  <hr>
  
  <p>that all works.  But it defines a new Child function (or <em>type</em>, if you like) for every instance of MyType1.  What I'd like to do is get access to the parent class scope, without resorting to that inefficiency.
  Something like this: </p>
  
  <pre><code>(function(){
      MyType2 = function(name){
          this.TypeName = "MyType2";
          this.Name = name;
          this.Prop1 = 1872;
          var parentName = name;
      };
  
      MyType2.prototype.SayHello = function() {
          say(this.Name + " says hello...");
      };
  
      var c1 = function(name) {
          this.Description = parentName + "'s child, " + name;
          //                ^^ no go! ^^
      };
  
      c1.prototype.Introduce = function() {
          say(this.Description + ", greets you...");
      };
  
      MyType2.prototype.Child = c1;
  
  })();
  </code></pre>
  
  <p>But, this doesn't work.  The <code>parentName</code> var is out of scope, of course. </p>
  
  <p>Is there an efficient way for the Child instance (in the constructor, or in any class function) to gain access to the parent (MyType2) instance? </p>
  
  <hr>
  
  <p>I know that I could define the Child class to be an independent, non-nested class, then in the ctor for that, just pass the Parent instance. But this creates N references to the parent instance, one for every Child instance. That seems like an inefficiency I'd like to avoid. </p>
  
  <p>thanks for any tips.</p>
  
  <hr>
  
  <p><strong>EDIT</strong> - the reason I want the Child to have access to the parent instance, is that the parent holds an object that is fairly expensive to create - something like a db connection - and I'd like the child to be able to utilize that thing. </p>