title: In JavaScript, how does an object "obj" that doesn't have the property named "foo" be able to give a value using obj["foo"]?
body: <p>I read in the book Secrets of the JavaScript Ninja, that we can use <code>document.all["id"]</code> to get <strong>all</strong> elements in the document that has such ID, if the browser supports it.  (although supposedly we should only have one element with a particular ID).</p>
  
  <p>But <code>document.all</code> returns an <code>HTMLAllCollection</code> object, and I see in Chrome that it has 9 elements, like an array (9 elements, as set up in jsfiddle).  So I can understand why <code>document.all[9]</code> can return the element, but why can <code>document.all["foo"]</code> return it too?  If we define <code>var obj = { foo : 123 }</code>, then we can say <code>obj["foo"]</code>, but <code>document.all</code> is not an object with a key <code>foo</code>.  So supposedly, <code>document.all["foo"]</code> shouldn't be able to return an element like that.</p>
  
  <p><strong>Note: this question is not to ask about the use of <code>document.all</code>, and it is not about asking to have two elements with the same ID on the page.  It is asking why an object <code>obj</code> that does not seem to have the key <code>foo</code> is able to give a value using <code>obj["foo"]</code>  I do not know why this is not a valid programming question.</strong></p>
  
  <p>sample code:</p>
  
  <p>1 element with such ID:<br>
  <a href="http://jsfiddle.net/ArR5x/5/" rel="nofollow">http://jsfiddle.net/ArR5x/5/</a></p>
  
  <p>2 elements with such ID:<br>
  <a href="http://jsfiddle.net/ArR5x/10/" rel="nofollow">http://jsfiddle.net/ArR5x/10/</a></p>
  
  <p><strong>Update: Dan Tao is correct.  It is due to some properties being enumerable and some are not, and we can easily produce the same situation if ECMAScript 5 is supported: <a href="http://jsfiddle.net/Akdp9/12/" rel="nofollow">http://jsfiddle.net/Akdp9/12/</a> The is a valid question about JavaScript, and the real answer is that it is due to the enumerable attribute of a property.</strong></p>