TypeError: x is undefined - JS
<p>Im working on webgl app. I have <code>starsList</code> array and for some reason, when I run this function:</p>

<pre><code>function initTextures() {

        for (i = 0; i &lt; starsList.length; i++) {
            starsList[i].texture = gl.createTexture();
            starsList[i].texture.image = new Image();
            starsList[i].texture.image.onload = function () {
                handleLoadedTexture(starsList[i].texture)
            }
            starsList[i].texture.image.src = starsList[i].name + ".gif"; 
        }   
    }
</code></pre>

<p>I got this error:</p>

<pre><code>TypeError: starsList[i] is undefined
handleLoadedTexture(starsList[i].texture)
</code></pre>

<p>although <code>starsList[i].texture</code> defined at the first line in the loop.<br>
any ideas why?</p>