title: Javascript: Create a sub-class within a class
body: <p>I know Javascript doesn't support classes, so I'm creating a <code>Cube</code> object, then declaring its prototype methods. Within <code>Cube</code>, I want to create a <code>flip</code> object with more methods within it:</p>
  
  <pre><code>// Create Cube class
  var Cube = function(){
      this.x = 0;
      this.y = 0;
      //...
  }
  
  // Add methods to Cube prototype
  Cube.prototype = {
      initialize: function(){
          //...
      },
  
      // Adding sub-functions within flip
      flip: {
          start: function(){
              console.log(this.x); // This.x is undefined!
          },
  
          end: function(){
              console.log("Ending flip");
          }
      },
  
      getPos: function(){
          console.log(this.x); // Correctly outputs 0
      }
  
  // Declare instance of Cube
  var cubeInstance = new Cube();
  cubeInstance.flip.start();
  </code></pre>
  
  <p>However, I'm getting the wrong scope for <code>this</code> within <code>flip.start()</code>, because it outputs the <code>flip</code> object, instead of the <code>Cube</code> object I want: </p>
  
  <pre><code>Object { start: Cube.prototype.flip.start(), end: Cube.prototype.flip.end() }
  </code></pre>
  
  <p>What do you suggest I do so that <code>cubeInstance.flip.start();</code> will trigger the <code>start()</code> method, but I can still access the <code>Cube</code> variables (namely, this.x and this.y)?</p>