Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In JavaScript, you have function <em>statements</em> and function <em>expressions</em>. The first declare named functions, the latter evaluate to a named or anonymous function. You're using a function expression.</p> <p>What I think you want to do is a <em>call</em> expression. All you have to do is to immediately call your function that returns <code>i</code>.</p> <pre><code>function f() { var i, array = []; for (i = 0; i &lt; 3; i++) { // The extra parentheses around the function are unnecessary here. // But this is more idiomatic, as it shares syntax with how function // expressions are introduced in statements. // I.e. you could just copy-paste it anywhere. array[i] = (function () { return i; })(); // don't rely on automatic semicolon insertion } return array; } </code></pre> <p>Also note that, in your problematic example, all closures return <code>3</code> because all of them capture the same variable <code>i</code>.</p> <p>EDIT: To make the previous paragraph more clear, if you really wanted to have 3 distinct closures, you'd have to create a new scope for each one. Unlike other languages, Javascript doesn't create scope just by opening a block. It only creates scope in functions. Here are two ways of doing this:</p> <pre><code>function f() { var i, array = []; for (i = 0; i &lt; 3; i++) { // With a parameter in an inner function array[i] = (function (n) { return function () { // A new n is captured every time return n; }; })(i); } return array; } function f() { var i, array = []; for (i = 0; i &lt; 3; i++) { array[i] = (function () { // With a variable in an inner function var n = i; return function () { // A new n is captured every time return n; }; })(); } return array; } </code></pre> <p>The next example, however, is <strong>wrong</strong>, because even though <code>n</code> is declared in the <code>for</code> block, it will be as if it has been declared at the top of the function and only initialized in the <code>for</code> block. Remember, this is JavaScript, not Java, not C, not C#, not &lt;whatever bracketed language&gt;:</p> <pre><code>function f() { var i, array = []; for (i = 0; i &lt; 3; i++) { var n = i; array[i] = function () { return n; }; } return array; } </code></pre> <p>Equivalent code:</p> <pre><code>function f() { var i, array = []; var n; for (i = 0; i &lt; 3; i++) { n = i; array[i] = function () { return n; }; } return array; } </code></pre>
    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