Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As you said, the first part:</p> <pre><code>var arr = Array.apply(null, { length: 5 }); </code></pre> <p>creates an array of 5 <code>undefined</code> values.</p> <p>The second part is calling the <code>map</code> function of the array which takes 2 arguments and returns a new array of the same size.</p> <p>The first argument which <code>map</code> takes is actually a function to apply on each element in the array, it is expected to be a function which takes 3 arguments and returns a value. For example:</p> <pre><code>function foo(a,b,c){ ... return ... } </code></pre> <p>if we pass the function foo as the first argument it will be called for each element with</p> <ul> <li>a as the value of the current iterated element</li> <li>b as the index of the current iterated element</li> <li>c as the whole original array</li> </ul> <p>The second argument which <code>map</code> takes is being passed to the function which you pass as the first argument. But it would not be a, b, nor c in case of <code>foo</code>, it would be <code>this</code>.</p> <p>Two examples:</p> <pre><code>function bar(a,b,c){ return this } var arr2 = [3,4,5] var newArr2 = arr2.map(bar, 9); //newArr2 is equal to [9,9,9] function baz(a,b,c){ return b } var newArr3 = arr2.map(baz,9); //newArr3 is equal to [0,1,2] </code></pre> <p>and another one just to make it clearer:</p> <pre><code>function qux(a,b,c){ return a } var newArr4 = arr2.map(qux,9); //newArr4 is equal to [3,4,5] </code></pre> <p>So what about Number.call ?</p> <p><code>Number.call</code> is a function that takes 2 arguments, and tries to parse the second argument to a number (I'm not sure what it does with the first argument).</p> <p>Since the second argument that <code>map</code> is passing is the index, the value that will be placed in the new array at that index is equal to the index. Just like the function <code>baz</code> in the example above. <code>Number.call</code> will try to parse the index - it will naturally return the same value.</p> <p>The second argument you passed to the <code>map</code> function in your code doesn't actually have an effect on the result. Correct me if I'm wrong, please.</p>
    singulars
    1. This table or related slice is empty.
    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