title: Using named keys in a JS array
body: <p>I'm working through <a href="http://smile.amazon.com/Principles-Object-Oriented-JavaScript-Nicholas-Zakas/dp/1593275404/ref=sr_1_1?ie=UTF8&amp;qid=1442157201&amp;sr=8-1&amp;keywords=object+oriented+javascript">The Principles of Object-Oriented Javascript</a> and am confused with Zakas's use of a named key inside an array (as opposed to inside an object). See comment:</p>
  
  <pre><code>function EventTarget() {}
  
  EventTarget.prototype = {
  
      constructor: EventTarget,
  
      addListener: function(type, listener) {
  
          if (!this.hasOwnProperty("_listeners")) {
              // Why isn't this: `this._listeners = {};`
              this._listeners = [];
          }
  
          if (typeof this._listeners[type] === "undefined") {
              this._listeners[type] = [];
          }
  
          this._listeners[type].push(listener);
      },
  
      // more stuff
  }
  
  var target = new EventTarget();
  target.addListener("message", function(event) {
      console.log("Message is " + event.data);
  });
  </code></pre>
  
  <p>His code works fine (as it does if you substitute the array for an object literal), but my understanding has been that you should use an object if you want to access the contents by name. From the <a href="http://www.w3schools.com/js/js_arrays.asp">array article on w3schools</a>:</p>
  
  <blockquote>
    <p>Many programming languages support arrays with named indexes.<br>
    Arrays with named indexes are called associative arrays (or hashes).<br>
    JavaScript does <strong>not</strong> support arrays with named indexes.<br>
    In JavaScript, <strong>arrays</strong> always use <strong>numbered indexes</strong>.</p>
  </blockquote>
  
  <p>Is there a good reason that Zakas used an array like this? Can you please explain it? Alternatively, is this something I should submit to <a href="https://www.nostarch.com/oojs#updates">the errata</a>?</p>