title: How to create and event handler inside a Singleton class - Javascript
body: <p>I have the next code:</p>
  
  <pre><code>// Singleton
  var mySingleton = (function () {
      // Private methods and variables
  
      function privateMethod(){
          console.log( "I am private" );
      }
  
      var privateVariable = "Im also private";
  
     return {
        // Public methods and variables
        hello: function () {
          console.log( "I want to ''fire'' the event 'hello'" );
        }
      };
  })();
  </code></pre>
  
  <p>And I want to create my own event handling system. I'm tried with the following code:</p>
  
  <pre><code>var mySingleton = (function () {
  
      function privateMethod(){
          console.log( "I am private" );
      }
  
      var handlers = {};
      mySingleton.prototype.registerHandler = function(event, callback) {
         this.handlers[event] = callback;
      };
      mySingleton.prototype.fire = function(event) {
         this.handlers[event]();
      };
  
      var privateVariable = "Im also private";
  
      return {
        hello: function () {
          console.log( "I want to ''fire'' the event 'hello'" );
          fire('hello');
        }
      };
  
  })();
  </code></pre>
  
  <p>And I used as follows:</p>
  
  <pre><code>mySingleton .registerHandler('hello', function () {
      alert('Hi!');
  });
  </code></pre>
  
  <p>But this doesn't work... Throw me the next error:</p>
  
  <pre><code>Cannot read property 'prototype' of undefined
  </code></pre>
  
  <p>Anybody can help me, please?</p>
  
  <p><strong>MY CURRENT ERROR:</strong></p>
  
  <pre><code>Uncaught TypeError: Cannot set property 'mycustomevent' of undefined
  Uncaught TypeError: Cannot read property 'mycustomevent' of undefined
  </code></pre>