title: How do I create my own events in pure JavaScript (not DOM element events)?
body: <p>I have a lot of C# experience, and am trying to apply it to creating somewhat object-oriented client applications in JavaScript.  </p>
  
  <p>I've created a few modules in JavaScript, and I would like them to be able to each publish and listen to events from other modules.  However, I'm having trouble finding general documentation for creating/using events in JavaScript.  Everything I've found is about working with events defined on DOM elements, but I want to have pure JavaScript objects with events.</p>
  
  <p>In C#, events are a very basic kind of object member, which can be added to any class.  C# events are really syntactic sugar for three members on a class</p>
  
  <ol>
  <li>A "multicast delegate", which in JavaScript might roughly translate to an array of references to functions.  The delegate can then be "called", which will call each referenced function in a non-deterministic order.</li>
  <li>An <code>add</code> method to add listener functions to the delegate.</li>
  <li>A <code>remove</code> method to remove listener functions to the delegate.</li>
  </ol>
  
  <p>A simple JavaScript object capturing this pattern might look like this:</p>
  
  <pre><code>var MyEvent = {
      Listeners: [],
      Trigger: function () {
          for (var i= 0; i &lt; this.Listeners.length; i++) 
              Listeners[i]();            
      },
      Add: function(listener) { this.Listeners.push(listener); },
      Remove: function(listener) { this.Listeners.pop(listener); }
  }
  </code></pre>
  
  <p>However, I haven't been able to find any articles describing a pattern like this.  Is there a recommended way to accomplish something like this in Javascript?  Are there any subtleties of the language that the above implementation is overlooking?</p>