Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first two, and the third examples are equivalent, at the end they produce an <code>Array</code> object with only one <em>own property</em>, <code>length</code>, containing <code>5</code> as its value.</p> <p>When you call the <code>Array</code> constructor using a single numeric argument (like <code>Array(5);</code>), the newly created object will contain that number as its <code>length</code> property, the <em>index properties</em> aren't created:</p> <pre><code>var a = Array(5); a.hasOwnProperty('0'); // false </code></pre> <p>The second example produces just the same:</p> <pre><code>var a = []; a.length = 5; a.hasOwnProperty('0'); // false </code></pre> <p>About the third example, it isn't equivalent because it will create a property on the array object, even though its value is <code>undefined</code>:</p> <pre><code>var a = []; a[4] = undefined; a.hasOwnProperty('4'); // true </code></pre> <p>The fourth example:</p> <pre><code>var a = new Array(5); </code></pre> <p>Is just exactly the same as the second one (<code>var a = Array(5);</code>), there's no difference between using the <code>Array</code> constructor with or without the <code>new</code> operator, in the second example you are <a href="http://ecma262-5.com/ELS5_HTML.htm#Section_15.4.1" rel="nofollow">calling the <code>Array</code> constructor as a function</a>.</p> <p>And finally, about your <code>makeArrayToLength</code> function, by now I think you know isn't equivalent at all, since all the "index properties" are initialized to a "default" value. <sub>(BTW don't use <code>default</code> as Identifier, it's a Keyword...)</sub></p> <p>The <code>Array</code> constructor is usually avoided because it can have different behaviors depending the argument used, for example:</p> <pre><code>Array("5"); // one element array, (["5"]) Array(5); // empty array, length = 5 // v.s. ["5"] // one element array [5] // one element array </code></pre> <p>Also, the <code>Array</code> constructor could be overriden, while array literals will always work.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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