How to inject a variable as string?
<p>I'm working with a dynamically created function in a loop.</p>

<pre><code>for (var i = 0; i &lt; 4; i++) {
        window["functiontest" + i] = function () { alert(i); }
}
</code></pre>

<p>It works, but not how I want to get it working. Because when I do this, it means when the <code>functiontest0</code> will run it will alert "3" because it actually alerts <code>var i</code> while the loop has finished adding to <code>i</code>.</p>

<p>What I want is to somehow "hardcode" the current <code>i</code> so it will actually alert "0" and not "3". I mean something like this:</p>

<pre><code>window["functiontest" + i] = function () {
  // I need just the current state for `i` here and
  // not just the variable `i` - so for exampe I need
  // it as `i` literally put 0
}
</code></pre>

<p>Is there any way I can force it to write the result into a "string" or something else?</p>

<p>THX for help guys. and sorry for duplicate couldnt find anything while searching for it. mostly because i couldnt explain it that well :-)</p>

<p>but i ended up with something like this:</p>

<pre><code> for (var genfunc = 0; genfunc &lt; 4; genfunc++) {

   if (genfunc == 0) { //left
       window["keyDown" + sys_curcontrols[genfunc]] = (function (unique) {
           return function () { window["sys_keyLeft" + unique] = -1; }
       })(nid);
   }
</code></pre>
