title: I'm trying to wrap my head around variable scoping in Node.js and Vows
body: <p>As an exercise in TDD in Node.js, I'm trying to implement a very simple "database" that stores things as flat files.  Here's the very beginning of the DB module code:</p>
  
  <pre><code>var fs = require( 'fs' );
  
  exports.create = function( path, readycallback ) {
      new FlatFileDB( path, readycallback );
  };
  
  function FlatFileDB( path, readycallback ) {
      this.path = path;
      readycallback( null, this );
  }
  
  FlatFileDB.prototype.getPath = function() {
      return this.path;
  };
  </code></pre>
  
  <p>The DB creation is asynchronous, and in my test case, I have a check to see if calling the create() function actually results in two distinct objects:</p>
  
  <pre><code>var vows = require('vows'),
      assert = require('assert'),
      fs = require( 'fs' ),
      flatfileDB = require( '../lib/flatfileDB' );
  
  var twoDBs = {};
  
  vows.describe( 'flatfileDB' ).addBatch( {
      'creating the database': {
          topic: flatfileDB,
          'calling create': {
              topic: function( flatfileDB ) {
                  flatfileDB.create( './createTest', this.callback );
              },
              'results in an object with the path passed in': function( err, db ) {
                  assert.equal( db.getPath(), './createTest' );
              }
          },
          'calling create more than once': {
              topic: function( flatfileDB ) {
                  flatfileDB.create( './db1', function( err, newdb ) {
                      twoDBs.db1 = newdb;
                      flatfileDB.create( './db2', function( err, newdb ) {
                          twoDBs.db2 = newdb;
                          this.callback( null, twoDBs );
                      } );
                  });
              },
              'results in two objects with different paths.': function( err, dbs ) {
                  console.log( 'twoDBs.  db1: ' + twoDBs.db1 + ', db2: ' + twoDBs.db2 );
                  console.log( 'dbs: ' + JSON.stringify( dbs ) );
                  assert.notEqual( twoDBs.db1.getPath(), twoDBs.db2.getPath() );
              }
          }
      },
  }).run();
  </code></pre>
  
  <p>The output of those two console.log lines surprises me, though: </p>
  
  <pre>
  twoDBs.  db1: [object Object], db2: [object Object]
  dbs: {}
  </pre>
  
  <p>since I'm passing twoDBs to the test callback, I had expected dbs and twoDBs to be the some object, but they don't appear to be.  Can anyone help me out with what's going on here?</p>