Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Disclaimer</strong>: This is a very formal description of the above code - this is how <em>I</em> know how to explain it. For a simpler answer - check Zirak's great answer above. This is a more in depth specification in your face and less "aha".</p> <hr> <p>Several things are happening here. Let's break it up a bit.</p> <pre><code>var arr = Array.apply(null, { length: 5 }); // Create an array of 5 `undefined` values arr.map(Number.call, Number); // Calculate and return a number based on the index passed </code></pre> <p>In the first line, <a href="http://es5.github.io/#x15.4.1" rel="noreferrer">the array constructor is called as a function</a> with <a href="http://es5.github.io/#x15.3.4.3" rel="noreferrer"><code>Function.prototype.apply</code></a>. </p> <ul> <li>The <code>this</code> value is <code>null</code> which does not matter for the Array constructor (<code>this</code> is the same <code>this</code> as in the context according to 15.3.4.3.2.a. </li> <li>Then <code>new Array</code> is called being passed an object with a <code>length</code> property - that causes that object to be an array like for all it matters to <code>.apply</code> because of the following clause in <code>.apply</code>: <ul> <li>Let len be the result of calling the [[Get]] internal method of argArray with argument "length".</li> </ul></li> <li>As such, <code>.apply</code> is passing arguments from 0 to <code>.length</code> , since calling <code>[[Get]]</code> on <code>{ length: 5 }</code> with the values 0 to 4 yields <code>undefined</code> the array constructor is called with five arguments whose value is <code>undefined</code> (getting an undeclared property of an object).</li> <li>The array constructor <a href="http://es5.github.io/#x15.4.2.1" rel="noreferrer">is called with 0, 2 or more arguments</a>. The length property of the newly constructed array is set to the number of arguments according to the specification and the values to the same values.</li> <li>Thus <code>var arr = Array.apply(null, { length: 5 });</code> creates a list of five undefined values.</li> </ul> <p><strong>Note</strong>: Notice the difference here between <code>Array.apply(0,{length: 5})</code> and <code>Array(5)</code>, the first creating five times the primitive value type <code>undefined</code> and the latter creating an empty array of length 5. Specifically, <a href="http://es5.github.io/#x15.4.4.19" rel="noreferrer">because of <code>.map</code>'s behavior (8.b)</a> and specifically <code>[[HasProperty]</code> .</p> <p>So the code above in a compliant specification is the same as:</p> <pre><code>var arr = [undefined, undefined, undefined, undefined, undefined]; arr.map(Number.call, Number); // Calculate and return a number based on the index passed </code></pre> <p>Now off to the second part.</p> <ul> <li><a href="http://es5.github.io/#x15.4.4.19" rel="noreferrer"><code>Array.prototype.map</code></a> calls the callback function (in this case <code>Number.call</code>) on each element of the array and uses the specified <code>this</code> value (in this case setting the <code>this</code> value to `Number).</li> <li>The second parameter of the callback in map (in this case <code>Number.call</code>) is the index, and the first is the this value.</li> <li>This means that <code>Number</code> is called with <code>this</code> as <code>undefined</code> (the array value) and the index as the parameter. So it's basically the same as mapping each <code>undefined</code> to its array index (since calling <a href="http://es5.github.io/#x15.7.1" rel="noreferrer"><code>Number</code></a> performs type conversion, in this case from number to number not changing the index).</li> </ul> <p>Thus, the code above takes the five undefined values and maps each to its index in the array.</p> <p>Which is why we get the result to our code.</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. 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