title: JS - passing a connection(object) from one function to another
body: <p>I am new to JS and I have a simple question. I am writing a node_redis code to connect to the db.
  I have created a db module in which there is an init function to start the connection.
  the module also has another function which queries the db. for the query, i will need the connection (dbConnection) object from the first function and then use it in the 2nd function. how do i do this? I can get it done by using global variables but most places tell me its a bad idea.</p>
  
  <p>Sorry if the question is stupid, I am learning how to code. results in google tell me that it can be done by passing it as an object property. But i don't know if it is the proper way to do things in my context, or even how to do it.</p>
  
  <pre><code>var redis = require('redis'); 
  
  module.exports = redisDb = {
  // Initialize the module. Invokes callback when ready (or on error)
  init: function(config, callback) {
  
      // Open the database connection
      var dbConnection = redis.createClient(config.db.port, config.db.host, {no_ready_check: true}); 
  
      dbConnection.auth(config.db.authKey, function() {
          console.log("Connected!");
          console.log(dbConnection.keys('*'));
      });
  
      dbConnection.on('connect'     , log('connect'));
      dbConnection.on('ready'       , log('ready'));
      dbConnection.on('reconnecting', log('reconnecting'));
      dbConnection.on('error'       , log('error'));
      dbConnection.on('idle'         , log('idle...'));
      dbConnection.on('end'         , log('end'));
  
      function log(type) {
          return function() {
              console.log(type, arguments);
          }
      }
      callback("callback - Connected");
  },
  
  getValue: function(key, callback) {
      dbConnection.hgetall("hosts", function (err, obj) {
      console.dir(obj);
      });
  }
  };
  </code></pre>
  
  <p>EDIT:
  tried another way. still failing.</p>
  
  <pre><code>module.exports = redisDb = (function() {
  var config = require('../config');
  var redis = require('redis'); 
  
  return {
      connection: function(config) {
          var dbConnection = redis.createClient(config.db.port, config.db.host, {no_ready_check: true}); 
  
          dbConnection.auth(config.db.authKey, function() {
              console.log("Authenticated!");
          });
          return dbConnection
      },
      getValue: function(connection, callback) {
          connection.hgetall("hosts", function (err, obj) {
          console.dir(obj);
          });
      }
  }
  })();
  </code></pre>