Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>ECMAScript <em>does</em> have <a href="http://en.wikipedia.org/wiki/Associative_array" rel="nofollow">Associated Arrays</a><sup>1</sup> - Objects (and by extension, Arrays) are an example</p> <p><em>However</em>, <strong><a href="http://es5.github.com/#x15.4" rel="nofollow">some properties of Arrays are treated specially</a></strong>:</p> <blockquote> <p><em>Array objects give special treatment to a certain class of property names.</em> A property name <code>P</code> (in the form of a String value) is an array index if and only if <code>ToString(ToUint32(P))</code> is equal to <code>P</code> ..</p> <p>.. <em>Specifically, whenever a property is added whose name is an array index, the length property is changed, if necessary,</em> to be one more than the numeric value of that array index ..</p> </blockquote> <p>Thus, given <code>arr = []</code>, the expressions <code>arr["1"]</code> and <code>arr[1]</code> refer to the <em>same property</em> name. Since <code>P</code> (the property name) is "1" and <code>length</code> is 0 from above, then assignment to such property will set <code>arr.length</code> to <code>ToUint32(P)+1</code>, or 2.</p> <p>It is <em>not</em> possible to change this behavior. If you wish to <em>not</em> have a special <code>length</code> property, then use a "normal" Object instead of an Array. However, many of the <code>Array.prototype</code> functions <em>can</em> be used with arbitrary objects (with some implementation quirks aside) that have a <code>length</code> property and an Object <em>can</em> be created such that it uses <code>Array.prototype</code> as its own prototype.</p> <p>All that being said, the post <em>does not say what the real issue is</em>. Instead of supposing that it must be done in that particular manner, consider explaining what the intent is: e.g. why a["10"]? And what is wrong if there are "11 items" if the object will be used in a List?</p> <hr> <p><sup>1</sup> Please read the article before debating this statement: the term "Array" in the name does not imply an ordered sequence nor does it preclude an <em>additional</em> notion of a Length or the use of Hashing, etc. If you are going by a <em>different</em> definition, make sure to specify what it is and what the desired behavior is for a given operation.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload