How to implement a simple prioritized listener pattern in JavaScript
<p>I have an event/listener manager that has this function:</p>

<pre><code>  var addListener = function(event, listener) {
    myListeners[event].push(listener); //assume this code works
  }
</code></pre>

<p>But now I need to change it so that it looks like this:</p>

<pre><code>  var addListener = function(event, listener, fireFirst) {
    if(fireFirst) {
      myListenersToFireFirst[event].push(listener);
    } else {
      myListenersToFireSecond[event].push(listener);
    }
  }
</code></pre>

<p>This is so that when the <code>fireEvent</code> function is called, it will fire the listeners in the <code>myListenersToFireFirst</code> array first, then the listeners in the second array.</p>

<p>So it will look something like this:</p>

<pre><code>  var fireEvent = function(event) {
    var firstListeners = myListenersToFireFirst[event];
    //for each listener in firstListeners, call `apply` on it

    var secondListeners = myListenersToFireSecond[event];
    //for each listener in secondListeners, call `apply` on it
  }
</code></pre>

<p>Is this the best way to accomplish this in JavaScript? Is there a more elegant way of achieving this priority list of listener-event firing?</p>
