title: Node Event Emitter Example
body: <p>I have, what I think, to be a fairly trivial example of doing what I think might be the Observer pattern while using Node's EventEmitter. That said, I'm still getting my sea legs with Node. When I reviewed this with another developer - it appears right but seems slightly weird. </p>
  
  <p>The weird part is how the LightBulb object is listening to the LightSwitch object. AFAIK, the code works but I feel like I'm doing this wrong. </p>
  
  <pre><code>/*jslint node: true */
  
  'use strict';
  
  var events = require('events');
  var util = require('util');
  
  var LightSwitch = function(state) {
      var self = this;
      self.state = typeof state !== 'undefined' ? state : 'off';
  };
  
  util.inherits(LightSwitch, events.EventEmitter);
  
  LightSwitch.prototype.changeState = function() {
      var self = this;
      // FIXME use if else
      if (self.state == 'on') {
          self.state = 'off';
          self.emit('off');
      } else if (self.state == 'off') {
          self.state = 'on';
          self.emit('on');
      }
  };
  
  var LightBulb = function(LightSwitch) {
      var self = this;
      self.state = 'off';
      self.switch = LightSwitch;
  
      self.switch.on('on', function() {
          self.state = 'on';
      });
  
      self.switch.on('off', function() {
          self.state = 'off';
      });
  };
  
  LightBulb.prototype.currentState = function() {
      var self = this;
      console.log('The switch is ' + self.switch.state);
      console.log('The light bulb is ' + self.state);
      console.log('\n');
  }
  
  var hallwayLightSwitch = new LightSwitch();
  
  var hallwayLightBulb = new LightBulb(hallwayLightSwitch);
  
  // Default!
  console.log('--------- EVERYTHING IS OFF --------------');
  console.log('Switch is ' + hallwayLightBulb.switch.state);
  console.log('Bulb is ' + hallwayLightBulb.state);
  console.log('\n');
  
  // Flick it!
  console.log('------------ LIGHTS ON! ------------------');
  hallwayLightSwitch.changeState();
  hallwayLightBulb.currentState();
  
  // Flick it - again!
  console.log('------------ LIGHTS OFF! -----------------');
  hallwayLightSwitch.changeState();
  hallwayLightBulb.currentState();
  
  // And, flick it - again!
  console.log('------------ LIGHTS ON! -----------------');
  hallwayLightSwitch.changeState();
  hallwayLightBulb.currentState();
  </code></pre>
  
  <p>The console output is then ... as I expect:</p>
  
  <pre><code>[terryp@jinx] playing_with_events (master) :: node 02_lightswitch.js
  --------- EVERYTHING IS OFF --------------
  Switch is off
  Bulb is off
  
  
  ------------ LIGHTS ON! ------------------
  The switch is on
  The light bulb is on
  
  
  ------------ LIGHTS OFF! -----------------
  The switch is off
  The light bulb is off
  
  
  ------------ LIGHTS ON! -----------------
  The switch is on
  The light bulb is on
  </code></pre>